button button f

Welcome to button button f

ruby on rails - HTML code inside buttons with simple_form - Stack Overflow

2024.09.24 19:36


Skip to main content Stack Overflow About Products OverflowAI Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand OverflowAI GenAI features for Teams OverflowAPI Train & fine-tune LLMs Labs The future of collective knowledge sharing About the company Visit the blog

current community

Stack Overflow help chat Meta Stack Overflow

your communities

Sign up or log in to customize your list.

more stack exchange communities

company blog Log in Sign up Home Questions Tags Users Companies Labs Jobs Discussions Recent Tags Collectives

Communities for your favorite technologies. Explore all Collectives

Teams

Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat.

Learn more Explore Teams Teams

Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs

HTML code inside buttons with simple_form

Ask Question Asked 12 years, 3 months ago Modified 5 years, 1 month ago Viewed 25k times 36

I'm new to rails, and just found the simple_form gem. I installed it with bootstrap suport, but now I can't get this code to work the way I want it

%= f.button :submit, " i class='icon-ok icon-white' /i Save", class: "btn btn-primary" %

I just want to put the icon inside the button, but when I do this, it shows me a button with the text ' i class='icon-ok icon-white' /i Save'

I also tried to do

%= f.button :submit, class: "btn btn-primary" do % i class="icon-ok icon-white" /i Save % end %

But with no success. How can I add some HTML inside the button with simple_form gem?

ruby-on-rails ruby twitter-bootstrap simple-form Share Improve this question Follow edited May 6, 2015 at 13:29 Ismael 16.7k 6 6 gold badges 62 62 silver badges 76 76 bronze badges asked Jun 1, 2012 at 23:37 Bruno Campos Bruno Campos 2,188 1 1 gold badge 20 20 silver badges 34 34 bronze badges Add a comment |

5 Answers 5

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 71

Don't use content_tag. The following works:

%= button_tag(type: 'submit', class: "btn btn-primary") do % i class="icon-ok icon-white" /i Save % end % Share Improve this answer Follow answered Jun 5, 2012 at 14:24 Undistraction Undistraction 43.7k 62 62 gold badges 202 202 silver badges 334 334 bronze badges 8 1 Even if this solution works, I don t like it because I can t see the f variable anymore :( – Cyril Duchon-Doris Commented Nov 23, 2014 at 23:44 1 @CyrilDD Why do you need to see the form builder so badly? All it is is a collection of methods to simplify building a form, using a model or class as reference. A submit button has no need to know about the model s structure like inputs do, so there is actually little reason to use the builder. Unfortunately Rails s handling of most of the view tier is a complete disaster and woefully in need of updating. There are far bigger issues. – Undistraction Commented Nov 24, 2014 at 0:48 I don t need it so badly . It s just tha in my code, I have many %= f.something % and for some reason a %= button_tag % shows up. Somewhat ugly. However, this (may ?) become a real problem for submitting nested forms – Cyril Duchon-Doris Commented Nov 24, 2014 at 0:50 @CyrilDD It s not a problem with nested forms, as you are actually submitting a single form with a single submit button. No matter how nested it is, it s just clever use of a single form. Take a look at the rendered form. You ll see all the nesting is just encoded into the name attributes of the inputs and decoded at the other end. – Undistraction Commented Nov 24, 2014 at 0:52 But what about forms inside forms ? Then we d want to know which form we want to send (though I can t think of any scenario where one would do this maybe if we just want to post a subset of changes ?) – Cyril Duchon-Doris Commented Nov 24, 2014 at 1:05 | Show 3 more comments 27

In simple_form 3.0rc use :button button type (it passes your block to original ActiveView button helper):

%= f.button :button do % i class="icon-save" /i Commit % end %

Or write additional button wrapper.

For additional info look into simple_form/form_builder.rb FormBuilder#button method.

Share Improve this answer Follow answered Aug 28, 2013 at 13:14 Bombazook Bombazook 495 4 4 silver badges 14 14 bronze badges 5 Tried it, doesn t work. Writing a button wrapper sounds like a good idea, though. – Andrew Smith Commented Sep 1, 2013 at 13:29 For some reason this only works with :button, and not :submit. I m using 3.1rc2 – Petercopter Commented Sep 25, 2014 at 22:33 When you use button method it passes your block to #{type}_button or #{type} helper. Simple form button helpers doesn t support blocks so using button_button helper that declared as alias_method :button_button, :button we call original method from ActionView::Helpers::FormBuilder . With :submit button type it s not possible. Read code of simple_form/form_builder.rb for better explanation. – Bombazook Commented Sep 29, 2014 at 14:41 this works, and should be the actual answer that solves it since it uses simple_form methods. – Andrew Commented Sep 21, 2015 at 19:40 This should be the accepted answer. github.com/plataformatec/simple_form#buttons – Erowlin Commented Aug 4, 2017 at 7:26 Add a comment | 7

I think you can't do it with simple_form. But I have good news for you. You should be fine using rails helper along side with simple form.

just do

button_tag(type: 'submit', class: "btn btn-primary") do content_tag(:i, class: "icon-ok icon-white") "Save" end

Not sure if this works, even the syntax but it should give you a hint

Share Improve this answer Follow edited Nov 7, 2013 at 22:22 answered Jun 2, 2012 at 2:09 Ismael Ismael 16.7k 6 6 gold badges 62 62 silver badges 76 76 bronze badges Add a comment | 1

One line example submit button in Rails with bootstrap btn class:

%= button_tag(type: 'submit', class: "btn btn-primary") do % Save % end % Share Improve this answer Follow answered Feb 17, 2017 at 1:51 jasonleonhard jasonleonhard 13.5k 1 1 gold badge 94 94 silver badges 69 69 bronze badges Add a comment | 0

You can do this with the following code:

= f.button :button, 'Send', data: { disable_with: " i class='fi-heart' /i Sending " }

Note that you want to use f.button instead of f.submit Also note that :button needs to be the first param to f.button

Share Improve this answer Follow answered Jun 25, 2016 at 13:34 stephenmurdoch stephenmurdoch 34.5k 29 29 gold badges 118 118 silver badges 193 193 bronze badges 1 1 Also note that :button needs to be the first param thats solved my problem. i tried using :submit, which didn t work! it didn t parse the text to html, but when i changed to :button it worked perfectly. thanks – Ziv Galili Commented Dec 15, 2016 at 16:14 Add a comment |

Not the answer you're looking for? Browse other questions tagged ruby-on-rails ruby twitter-bootstrap simple-form or ask your own question .

The Overflow Blog Where developers feel AI coding tools are working—and where they’re missing He sold his first company for billions. Now he’s building a better developer Featured on Meta User activation: Learnings and opportunities Preventing unauthorized automated access to the network Announcing the new Staging Ground Reviewer Stats Widget Should low-scoring meta questions no longer be hidden on the Meta.SO home

Linked

29 Rails and bootstrap - Add HTML tags to a submit button text 1 How to show a twitter bootstrap icon in RoR? 1 In Ruby on Rails, how to customize the submit button to use BootStrap and add a icon to it? -3 Rails 4 - Bootstrap themes and crazy CSS

Related

2 HTML (or Rails) button code issue 0 ruby on rails using bootstrap - prepend text and append button to an input field in a form 29 Rails and bootstrap - Add HTML tags to a submit button text 1 Ruby on Rails, Bootstrap& Buttons 3 Simple Form Radio Buttons - Display inline 1 Format a button in form_tag 2 Bootstrap button group and form_for 9 How to add a switch toggle button to simple form in rails 0 simple_form bootstrap input 1 Rails simple form button text

Hot Network Questions

\usepackage[ language ]{babel} or \babelprovide{ language }? Why? Is BitLocker susceptible to any known attacks other than bruteforcing when used with a very strong passphrase and no TPM? In the absence of an agreement addressing the issue, is there any law giving a university copyright in an undergraduate student's class paper? VLA configuration locations What is the smallest interval between two palindromic times on a 24-hour digital clock? How can the doctor measure out a dose (dissolved in water) of exactly 10% of a tablet? Breeding Fish Minecraft Figure out which of your friends reveals confidential information to the media! Merge digits into numbers within a list Should punctuation (comma, period, etc.) be placed before or after the inches symbol when listing heights? If a professor wants to hire a student themselves, how can they write a letter of recommendation for other universities? tokens available in an email Is there a way to have my iPhone register my car which doesn't have carplay, only for the Car is parked at -feature? How was the year spoken in late 1800s England? Do always prepared Spells cost spell slots? Bridge in a walled garden Need help with a system of 2 very verbose equations Expected value of a matrix = matrix of expected value? Starter circuit for small 12 V solar panel/submersible water pump What should you list as location in job application? Can Inductors be thought of as storing voltage? Why is China not mentioned in the Fallout TV series despite its significant role in the games' lore? What kind of epistemology would justify accepting religious claims that lie beyond the reach of scientific and historical verification? Want a different order than permutation more hot questions Question feed

Subscribe to RSS

Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-rb
Stack Overflow
Questions Help Chat
Products
Teams Advertising Talent
Company
About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Settings Cookie Policy
Stack Exchange Network
Technology Culture & recreation Life & arts Science Professional Business API Data Blog Facebook Twitter LinkedIn Instagram

Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2024.9.23.15701



Vivamus fermentum nibh