Scott S.
Scott S.

Reputation: 759

Rails visually display multiple fields as one link

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

Answers (3)

Wim
Wim

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

TimPetricola
TimPetricola

Reputation: 1491

You can do it simply with concatenation:

<%= link_to "#{employee.first} #{employee.last}", employee_path(employee) %> %>

Upvotes: 3

ksol
ksol

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

Related Questions