简体   繁体   中英

rails link_to_function in a do

I have a link_to_function working correctly in my rails app, but I've tried wrapping it around some ruby code using "do", and it's not executing the code it's wrapped around:

<li class="comment-list-item" id="<%=comment.id%>">
<%= link_to_function "test", "goToStepComment('#{step.id}', '#{comment.id}')" do %> 
    <div class="comment-list-image">
        <% if User.find(comment.user_id).avatar_url != nil %>
              <%= image_tag(User.find(comment.user_id).avatar_url(:thumb), :class=>"commentAvatar img-polaroid") %>
        <% else %>
              <%= image_tag("default_avatar.png", :class=>"commentAvatar img-polaroid") %>
        <% end %>
    </div>
    <div class="comment-list-title">
          <p><%= truncate(User.find(comment.user_id).username, length: 13) %> commented on <%= truncate(step.name, length: 20) %>: "<%=truncate(comment.body)%>"</p>
    </div>
<% end %>
</li>

This gets rendered as:

<li class="comment-list-item" id="40" style="display: list-item;">
    <a href="#" onclick="goToStepComment('114', '40'); return false;">test</a>     
</li>

How do I wrap a link_to_function around ruby code?

<p><%= truncate(User.find(comment.user_id).username, length: 13) %> commented on <%= truncate(step.name, length: 20) %>: "<%=truncate(comment.body)%>"

In the above code that you have used, why is the last portion, <%=truncate(comment.body)%> being wrapped around double quotes. I tried the same with a sample code as given below.

<li class="comment-list-item" id="1">
<%= link_to_function "test", "goToStepComment('12', '1222')" do %> 
    <div class="comment-list-image">
    <p>Something</p>
    </div>
    <div class="comment-list-title">
          <p>FooBar</p>
    </div>
<% end %>
</li>

This produced below given html content.

<li id="1" class="comment-list-item">
    <a onclick="goToStepComment('12', '1222'); return false;" href="#">test</a>
</li>

I think the error would be arising from the double quotes that you have given in the code mentioned at the top.

我摆脱了link_to_function并最终在用户单击li元素时调用了goToStepComment函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM