0

This has to be really simple but I'm stuck.

I have two fields fname and lname that I want to put together and use as the link to the Show method.

<%= link_to 'Show', person %>
<%= link_to person.fname %> <%= person.lname %>

The top line links to where I need it to go. But, the second line displays the first and last name the way I want it to, though it doesn't link to the specific record, just the index page.

How can I have the second line link to the specific record?

Thanks

Scott

Scott S.
  • 749
  • 1
  • 7
  • 26
  • Just so you know, the first argument to `link_to` can be a string, and you can make up your string however you want i.e. combining two strings, some other expression, etc. – regulatethis Dec 29 '12 at 00:04

2 Answers2

1

Your best option would be to create an attribute on your personmodel for their full name:

def full_name
  self.fname + ' ' + self.lname
end

Then in your view, you can use:

<%= link_to person.full_name, person_path(person) %>

This is assuming that you've declared your routes properly inside routes.rb.

Zajn
  • 4,078
  • 24
  • 39
1

Try this:

<%= link_to person.fname + " " + person.lname, person %>

Whay you have inadvertantly done is split the link into to different parts by adding two <%= %> delimiters. What you need to do is join the fname and lname together as the first argument of the link_to, and they supply the object as the second argument so link_to knows what link to generate.

As an aside, a faster way to join strings together is to use interpolation:

<%= link_to "#{person.fname} #{person.lname}", person %>
Myles Eftos
  • 974
  • 6
  • 4