开发者

Rails - view: Ruby code in link_tag

开发者 https://www.devze.com 2023-03-23 18:56 出处:网络
I\'d like to have embededd ruby code as my link name. Right now I tried to implement it like this: <% @user.each do |user| %>

I'd like to have embededd ruby code as my link name. Right now I tried to implement it like this:

<% @user.each do |user| %>
    <li><%= link_to '<%= user.familyname %>, <%= user.forename %> ', user %> </li>
<% end %>

But it's not working, Rails gives me a syntax error:

syntax error, unexpected $undefined, expecting ')'
...);@output_buffer.safe_concat('\', user %> </li>
...                               ^

What do I need to change in the syntax, 开发者_StackOverflow社区so this Link will work?


you can not do <%= %> inside an <%= %>. you can try something like this:

    <% @users.each do |employee| %>
        <li><%= link_to "#{employee.familyname}, #{employee.forename}", employee %> </li>
    <% end %>


Just simple

<%= link_to [user.familyname, user.forename].join(','), user_path %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号