Forum: Ruby on Rails link_to: Space in between 2 variables used for anchor

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeremy L. (Guest)
on 2007-07-30 04:54
I have this code to create a link

<b>Employee:</b> <%= link_to({@employee.first_name,
@employee.last_name}, :controller => 'employees', :action => 'show', :id
=> @employee.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 L."?
Robby R. (Guest)
on 2007-07-30 06:24
(Received via mailing list)
Jeremy L. 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
> => @employee.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 L."?

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
   end
end

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
end

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

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

Cheers,
Robby

--
Robby R.
http://www.robbyonrails.com/
http://www.planetargon.com/
Davi (Guest)
on 2007-07-31 21:30
(Received via mailing list)
Em Domingo 29 Julho 2007 23:24, Robby R. escreveu:
> Do one of the following things.
>
> # Add a full_name method to your Employee model
> # Add a helper to render the full name
>
[...]

  Can't I do the follow?

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


  Thanks

--
Davi V.
removed_email_address@domain.invalid
removed_email_address@domain.invalid
--
This topic is locked and can not be replied to.