简体   繁体   English

如何在rails中为link_to标签设置唯一的ID?

[英]how to set an unique id for link_to tag in rails?

I need to separate unique ids for some linking purpose, I just want to know how to make an unique id for link_to ? 我出于某些链接目的需要分离唯一ID,我只想知道如何为link_to制作唯一ID?

Like I am using: 就像我在使用:

<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, :id => @question.id%>

This didn't work then I tried: 这没有用,然后我尝试了:

<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, id: myBtn_<%=question.id%> %>

Nothing worked. 没事。 Is there any solution? 有什么解决办法吗?

Try with: 尝试:

<%= link_to 'Edit', 
  edit_question_path(question.id),
  method: :get, 
  remote: true, 
  id: "myBtn_#{question.id}" %>

You need to see what's the way to access a question variable, as instance or local one (I used a question , as you're doing with the path), and myBtn_<%=question.id%> won't work if you don't have defined a myBtn variable, even less if you try to include erb tags to print inside the erb open-close for the link_to . 您需要查看以何种方式访问​​问题变量(例如实例还是本地变量)(我使用了question ,因为您正在处理路径),而如果您使用myBtn_<%=question.id%>它将无法正常工作尚未定义myBtn变量,即使您尝试包含erb标签以在link_to的erb开-关内部打印,也没有定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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