Reputation: 759
I have first name and last name in separate fields in my database. I'd like to display them next to each other and have the combined string be the link to the show method.
Right now this is what I have:
<td><%= link_to employee.first, employee_path(employee) %></td>
<td><%= employee.last %></td>
Which displays:
Mark Smith (with the link on Mark)
I'd like it to display as:
Mark Smith (with both words as the hyperlink)
Upvotes: 0
Views: 185
Reputation: 1096
First get the total name in the controller
@employee_name = employee.first + " " + employee.last
In the erb, put:
<%= link_to @employee_name, employee_path(employee) %>
Upvotes: 0
Reputation: 1491
You can do it simply with concatenation:
<%= link_to "#{employee.first} #{employee.last}", employee_path(employee) %> %>
Upvotes: 3
Reputation: 12275
All you have to do is concatenate both strings employee.first
& employee.last
. You can do it directly in your view, or add a method in your model that does it for your, or add a helper. I'd go with the third option.
Upvotes: 3