John Abraham
John Abraham

Reputation: 18781

Link_to in rails and dynamic names from modal#show

I have a loop that creates a list of works from the modal work

//does work but want test to be <%= work. name %>

<ol class="meny-control mobile"> 
   <% @works.each do |work| %>
       <li class="" data-id="<%= work.id %>"><%= link_to 'test', work %></li>
   <% end %>
</ol>

//doesnt work but want it to

<ol class="meny-control mobile"> 
       <% @works.each do |work| %>
           <li class="" data-id="<%= work.id %>"><%= link_to '<%= work.name %>', work %></li>
       <% end %>
    </ol>

As you would guess the <%= work.name %> throws a syntax error. How do I correctly format the link_to to display each work.name as the 'path' && the anchor's inner html as work.name.

Being new to rails, I'm still really iffy on understanding documentation properly. Could you please reference from link_to() (if even there) where this format is explained so I use this for future referencing & understanding --also how to properly edit the stack question title for future similar question.

Upvotes: 1

Views: 1728

Answers (2)

Zakaria
Zakaria

Reputation: 1013

you don't need "#{}".

you can write this:<%= link_to work.name, work %>

Upvotes: 3

vee
vee

Reputation: 38645

The error is because of the nesting of <% tags and I suppose you already are aware of that. To solve your problem use the following:

<%= link_to "#{work.name}", work %>

The #{} is used to interpolate variables, i.e. replacing variables with their values within the string literals as in link_to "#{work.name}" above where work.name will be replaced by the value work.name holds.

Upvotes: 7

Related Questions