link_to: Space in between 2 variables used for anchor

Jeremy Luebke wrote:

I have this code to create a link

<b>Employee:</b> <%= link_to({@employee.first_name,
@employee.last_name}, :controller => 'employees', :action => 'show', :id
=> %>

It created a link with the anchor text of "FirstLast". How do I put a
space in between the two so it looks like "First Last"?

Do one of the following things.

# Add a full_name method to your Employee model
# Add a helper to render the full name

I prefer the first option...

class Employee
   # ... etc

   def full_name
     first_name + ' ' last_name

Then you get... @employee.full_name in your code.

<%= link_to( @employee.full_name, :controller => ... ) %>

The helper route?

# add to a helper file that your view can access
def full_name(employee)
   employee.first_name + ' ' + employee.last_name

<%= link_to( full_name(@employee), :controller => ... ) %>

Either of those should work for you. I prefer to see something like this in the model.



  Can't I do the follow?

<%= link_to("#{employee.first name} #{employee.last_name}", :controller
=> 'employees', :action => 'show', :id => %>