Frage

I know that if you just type something like <button>Something</button> outside a form_for in rails, it will create a useless button. But I want to create buttons within this form_for to be handled by JavaScript. Is there a way to create it?

War es hilfreich?

Lösung

This will create useless buttons that can be handled by JavaScript.

Plain HTML:

<input type="button" onclick="alert('Hello.')" value="Click Here" />

Rails:

<%= submit_tag "Click Here", :type => 'button', :onclick => 'alert("Hello.")' %>

Andere Tipps

If you're not looking for Rails to use it, why not just use the plain html inside the form_for?

<%= form_for @record do |f| %>

  ## inputs ##

  <button>Something</button>
  <%= f.submit %>

<% end %>

Check out this answer: How do I create multiple submit buttons for the same form in Rails?

<% form_for(something) do |f| %>
    ..
    <%= f.submit 'A' %>
    <%= f.submit 'B' %>
    ..
<% end %>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top