Bastien
Bastien

Reputation: 606

rails button_tag vs. button_to - class & id

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

Answers (1)

Pavan
Pavan

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

Related Questions