Embed HTML in the rails button_to button bar

Possible duplicate:
Is there a way using a helper to create button tags for sending

This is probably a pretty simple question, but I have this HTML layout:

<button class="btn btn-large btn-inverse"><i class="icon-white icon-shopping-cart"></i> Add to bag</button> 

This is the add to cart button, I follow the Agile Web Development book to create a cart. The question is, with embedded HTML between the button tag, how can I display it like that? I tried this:

 %= button_to raw('<i class="icon-white icon-shopping-cart"></i> Add to bag'), line_items_path(product_id: product) %> 

But firstly, it prints "enter", not a button, and the text is all distorted in html, and not between the opening and closing tags.

I know that I can just write all this manually, but I was hoping to find out if there is a way to shorten this HTML code for output using rails labels, mainly because with twitter bootstrap I always see this type of HTML template ...

Any help is appreciated.

+4
source share
1 answer

As Mischa commented, it is similar to Is there a way to create a helper to create a button tag to send .

The content_tag helper can be used to write buttons in rails. However, this is not a shortcut, as the entry will take longer than the original html.

However, if you need to do this often, you can write your own helper method to do it the way you want!

Just add / helpers / application _helper.rb to your application

 def my_button_to name, options = {}, html_options = {} # or some variation # eg. deal with options hash the way button_to deals with it here? content_tag :button, html_options = nil do raw name end end 

Now you can use this in your views:

 <%= my_button_to '<i class="icon-white icon-shopping-cart"></i> Add to bag', {}, :class => "btn btn-large btn-inverse" %> 
+1
source

All Articles