Reputation: 606
I am trying to build a button with an id and a class with rails. A javascript event fires when the button get clicked. I tried different things none of them ticks all the boxes.
<%= button_tag "Launch the js event", id: "button-feedback-by-bullet" %>
This works but there is no class
<%= button_tag "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %>
I get an error saying there are too many arguments
<%= button_to "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %>
I get the right class but the button pass a post method and the js event does not get fired
Could you please help me find the right syntax and understand nuances between button_tag and button_to? Thanks.
Upvotes: 0
Views: 3493
Reputation: 33542
button_tag(content_or_options = nil, options = nil, &block) public
You should do
<%= button_tag "Launch the js event", {id: "button-feedback-by-bullet", class: "btn-primary"} %>
or with button_to
button_to(name = nil, options = nil, html_options = nil, &block)
<%= button_to "Launch the js event", {}, {id: "button-feedback-by-bullet", class: "btn-primary"} %>
Also, button_tag
or button_to
triggers a submit
by default, perhaps you should use link_to
instead
Upvotes: 1