Justin D.
Justin D.

Reputation: 4976

Rails and bootstrap - Add HTML tags to a submit button text

I have a form for a like/unlike button using ajax :

= form_for like, :html => { :method => :delete}, :remote => true do |f|
= f.submit pluralize(@video.likes.count, 'like'), :class => "btn btn-warning btn-mini", "data-disable-with"=> "Just a moment..."

The form works perfectly.

I would like to add an icon in front of the text in the submit button. The haml code for adding the icon is the following (twitter bootstrap) :

%i.icon-heart.icon-white

Is there a way to add this html to the button? I tried adding it as plain html, but rails rendered it as text.

UPDATE

I have manage to encapsulate the submit button in a span class which contains the icon and the appropriate styling. I now need to remove every styling on the button...

%span.btn.btn-danger.btn-mini
  %i.icon-heart.icon-white
  = f.submit pluralize(@video.likes.count, 'like')

Upvotes: 29

Views: 37161

Answers (7)

tommyalvarez
tommyalvarez

Reputation: 625

The accepted answer for the question itself will work but is more like a patch instead of the perfect solution keeping code style clear and consistent. It backtracks into using the standard and manual view method: #button_tag. I tend to avoid those manual methods like #submit_tag, #form_tag, #input_tag ... etc because they are unrelated to the backed model or form-model itself and you need to manually do everything on them. Even though the submit has no connection with the f. that every input in a form_for has like for e.g. f.input ..., it's about style, code readability and good programming practices. This code works perfectly fine (form_for and simple_form):

= f.button :button, type: :submit, class: 'class1 class2 ... classN' do
    = 'button call to action text'
    %i.fa.fa-download.ml5 // => haml icon as requested

Hope it helps others reaching this post like me trying to avoid _tag methods.

Upvotes: 6

Bhavnesh Choudhary
Bhavnesh Choudhary

Reputation: 1

Please try the below code . It works

<%= f.submit :class => "btn btn-success btn-mini" %>

Upvotes: -3

gilcierweb
gilcierweb

Reputation: 2714

Soluction

= button_to('Add', line_item_path, method: :post , class: "btn btn-warning btn-lg" , params: { param1: 'value1',  param2: 'value2' })

http://qiita.com/tomomomo1217/items/a5f790c31670587e2d87

How to place an image inside of a link?

Upvotes: 0

kangkyu
kangkyu

Reputation: 6090

Another option could be button in place of submit

see Rails documentation FormBuilder#button

= f.button :class => "btn btn-warning btn-mini" do
  %i.icon-heart.icon-white
    = pluralize(@video.likes.count, 'like')

Upvotes: 3

flanger001
flanger001

Reputation: 777

Justin D's answer helped me also. If you're coming here from Google and you're looking for a Slim implementation, you can do it like this:

= button_tag(type: 'submit', class: 'btn btn-default') do
    span.glyphicon.glyphicon-floppy-disk>
    | Save
end

Slim users will recognize the >'s necessity.

This worked for me with Rails 4.1.5, Ruby 2.1.2, and bootstrap-sass 3.2 as of September 24, 2014.

Upvotes: 2

Justin D.
Justin D.

Reputation: 4976

Thanks to ismaelga, I found this SO question.

This is the solution :

<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
 <i class="icon-ok icon-white"></i> Save
<% end %>

Upvotes: 100

Ismael
Ismael

Reputation: 16720

Try this. I haven't tested but I think it's possible to do something like this.

= f.submit :class => "btn btn-warning btn-mini", "data-disable-with"=> "Just a moment..." do
  %i.icon-heart.icon-white
  = pluralize(@video.likes.count, 'like')
end

So this was possible if you where using simple_form. I'm sorry.

So another try would be

= f.submit "#{pluralize(@video.likes.count, 'like')} <i class='icon-heart icon-white'/>".html_safe, :class => "btn btn-warning btn-mini", "data-disable-with"=> "Just a moment..."

Upvotes: 2

Related Questions