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.
C779ed55c2ac0c7957463580e9234a70?d=identicon&s=25 Jeremy Luebke (jeremyl)
on 2007-07-30 02: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 Last"?
B84d42a3a5c343f8fc6ab7d7f47fd3f5?d=identicon&s=25 Robby Russell (Guest)
on 2007-07-30 04:24
(Received via mailing list)
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
> => @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 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
   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 Russell
http://www.robbyonrails.com/
http://www.planetargon.com/
4cd3615da7c39beacaaab174267ad8b2?d=identicon&s=25 Davi (Guest)
on 2007-07-31 19:30
(Received via mailing list)
Em Domingo 29 Julho 2007 23:24, Robby Russell 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 Vidal
davividal@siscompar.com.br
davividal@gmail.com
--
This topic is locked and can not be replied to.