rails button_tag vs. button_to - class& ID

时间:2017-07-06 20:22:53

标签: javascript ruby-on-rails

我正在尝试构建一个带有id和带有rails的类的按钮。单击按钮时会触发javascript事件。我尝试了不同的东西,没有一个能打勾。

  <%= button_tag "Launch the js event", id: "button-feedback-by-bullet" %> 

这样可行,但没有课程

  <%= button_tag "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %> 

我收到一条错误消息,说明参数太多

  <%= button_to "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %>

我得到了正确的类,但按钮传递了post方法,并且js事件没有被触发

你能帮我找到正确的语法并理解button_tag和button_to之间的细微差别吗?感谢。

1 个答案:

答案 0 :(得分:1)

  

button_tag(content_or_options = nil,options = nil,&amp; block)public

你应该做

<%= button_tag "Launch the js event", {id: "button-feedback-by-bullet", class: "btn-primary"} %>

button_to

  

button_to(name = nil,options = nil,html_options = nil,&amp; block)

<%= button_to "Launch the js event", {}, {id: "button-feedback-by-bullet", class: "btn-primary"} %>

此外,button_tagbutton_to 默认触发submit ,也许您应该使用link_to而不是