Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 13949

`button_tag` instead of `submit_tag`

I would like to include a glyphicon in my form submit button. It isn't possible to include glyphicons inside submit_tag, so I used button_tag.

However, in some forms I have different submit_buttons (like preview|for_real), and I used the:

button "commit"

message in the controller to have a specific action:

if params[:commit] == 'Preview'

which only works with submit_tag.

1. I'd like to understand the difference between the two.

Rails submit_tag

submit_tag "Edit this article"
# => <input name="commit" type="submit" value="Edit this article" />

and button_for

<%= button_tag(type: "submit", class: "btn btn-default") do %>
    Edit this article <i class="icon-repeat"></i>
<% end %>

Is the difference just that the input will also submit a commit message that does not allow HTML/glyphicons?

2. What is the workaround to have a submit_tag behavior (commit message) with a glyphicon inside?

Or is it not recommended to use the commit value, and something else should be used that would work with button_tag?

Upvotes: 5

Views: 3071

Answers (1)

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 13949

The solution is that there is actually no problem with the button_tag solution. Therefore submit_tag is just useless (until I find some other advantages it has over button_tag).

button_tag(name: 'commit', type: 'submit', value: 'Edit this article')
button_tag(name: 'commit', type: 'submit', value: 'Publish')

Although those are both rendered as <button> in the HTML, only the value of the one that was clicked will be passsed !

Upvotes: 6

Related Questions