0

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?

scientiffic
  • 9,045
  • 18
  • 76
  • 149
  • link_to_function has a murky history with many decisions on being deprecated or not being deprecated. As a result it really depends on which version you're running as to whether it exists and if it accepts a block. Which version of rails are you running? – trh Nov 19 '13 at 17:27
  • I'm running Rails 3.2.15 – scientiffic Nov 19 '13 at 17:43

2 Answers2

0
<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.

Deepak A
  • 322
  • 1
  • 3
  • 12
  • thanks for your help. I removed the double quotes, but that didn't solve the problem. It looks like in your example above, you also weren't able to wrap "Something" and "Foobar" into your output HTML, is that right? – scientiffic Nov 19 '13 at 17:42
  • 1
    Yes. I couldn't wrap those things up. And sorry, I didn't notice it as link_to_function. Instead of using link_to_function, you can use link_to. The click event you could attach by a javascript handler. I found some fuss around using link_to_function method in rails community and everyone pushing to use normal link_to with an event handler. Try that way too. – Deepak A Nov 19 '13 at 18:04
0

I got rid of the link_to_function and ended up just calling the function goToStepComment when the user clicks on the li element.

scientiffic
  • 9,045
  • 18
  • 76
  • 149