I am trying to add a class to a button generated with rails button_to helper, but I cannot find a consistent answer to the question of how to incorporate the class into the declaration. I have:
<%= button_to("Logout", session_url, method: :delete, class:'waves-effect waves-light btn-large') %>
Try this
<%= button_to("Logout", session_url, method: :delete, :class => "btn btn-success") %>
This works fine for me
You need to split the "html options" from the "make the button_to" options - ie make two separate hashes eg:
<%= button_to("Logout", session_url, {method: :delete}, {class:'waves-effect waves-light btn-large'}) %>
Try this:
<%= button_toγ"Logout", session_url, method: :delete, form_class:"waves-effect waves-light btn-large" %>
Related
I want to use an Emoji, e.g. Trash-Emoji (HTML Entity: & #x1f5d1;) inside link_to, like:
<%= link_to "π", tape_path(tape.id), method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' %>
But no Trash-Emoji itself is showing.
I also tried "& #x1f5d1;", #{& #x1f5d1;}, & #x1f5d1; but syntac errors were shown.
I want to try to write without using any additional GEM at first.
Does someone know how should be written?
html_safe and raw are not very flexible approaches when you want to make a really customized link
It is more elegant to use block where you can do A LOT:
<%= link_to root_path do %>
Homepage
<div class="fa fa-flag"></div>
π
<%= User.count %>
<% end %>
https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
Your case:
<%= link_to root_path, method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' do %>
π
<% end %>
I solved this problem to use "link_to raw" now!
<%= link_to raw("π").html_safe, tape_path(tape.id), method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' %>
What i got so far:
= button_to 'Delete', "/blog/#{#post.id}", method: 'delete', class: 'btn btn-danger'
However adding a confirm: 'Are you sure' Has no effect. Also that line is very ugly and i'd like to shorten it.
<%= button_to 'Delete', blog(#post), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>
I'm using rails v4.1.8. Anthony's solution does not work for me.
I needed to change the syntax a bit:
<%= button_to 'Delete', blog(#post), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure?' } %>
I have a RoR app using bootstrap. I'm trying to apply the fontawesome html icon tag to a submit_tag helper, but it does not seem to be supported. When I click submit, the disable content just appears as a string instead of being interpreted to html, though it does for link_to helper.
Here's the erb:
<%= form_tag("/home/search", method: "get", class: "form-inline", role: "search", remote: true) do %>
<div class="form-group">
<%= text_field_tag(:term, nil, {:class => "form-control", "data-html" => true, :value => #term}) %>
</div>
<%= submit_tag "Go!", class: "btn btn-transparent", role: "button", 'data-disable-with' => "<i class='fa fa-spinner fa-spin'></i> Searching...".html_safe %>
<% end %>
Here's what it comes out looking like when I click submit:
It works with link_to, but then I can't pass the value from the text_field_tag to link_to otherwise, I'd be happy with that solution. I'd really rather avoid writing the .ajax method myself and using javascript to manipulate button values. Any suggestions on how to solve this with the standard FormHelper tags? Many Thanks in advance.
You should try to change submit_tag to button_tag, something like this:
<%= button_tag "Go!", class: "btn btn-transparent", 'data-disable-with' => "<i class='fa fa-spinner fa-spin'></i> Searching...".html_safe %>
If using simple_form:
<%= f.button :button,
'Save',
class: 'my-class',
data: {
disable_with: '<i class="fa fa-spinner fa-spin"></i>'
} %>
I've been using Rails for almost 10 years and I just stumbled across disable_with... neat!
You may also use the content_tag helper if you'd rather not insert raw HTML... for example (Simple Form):
= form.button :button,
t('.submit'),
class: 'btn btn-primary btn-block', \
data: { \
disable_with: [
content_tag(:span, '', class: 'spinner-grow spinner-grow-sm'), \
content_tag(:span,'Please wait...') \
].join \
}
Of course its probably cleaner to refactor into a helper...
# frozen_string_literal: true
module ApplicationHelper # :nodoc:
def disable_with_element(text = t('please_wait'))
[
content_tag(:span, '', class: 'spinner-grow spinner-grow-sm'),
content_tag(:span, text)
].join
end
end
= form.button :button,
t('.submit'),
class: 'btn btn-primary btn-block', \
data: { \
disable_with: disable_with_element \
}
Note, I'm using Simple Form and Slim in these examples.
A quick tip on generating a button_to with html_safe and disable_with:
<%= button_to some_path, method: :post, class: 'btn btn-md btn-primary', "data-disable-with": '<span class="fa fa-2x fa-spinner fa-spin"></span>'.html_safe do %>
<span class="fa fa-2x fa-facebook-square"></span>
<% end %>
My approach:
<%= submit_tag 'Submit, data: { disable_with: 'Processing...' } %>
At the top of the user's profile I have this button:
<%= button_to 'Make a detailed enquiry', user_path(#user), :class=>"enquirySection", :method => :get %>
Then at the very bottom of the page I am rendering a form in which the viewer can make an enquiry:
<div class="enquirySection">
<%= render "enquiries/form" %>
</div>
I want the user to be scrolled down to this form when (s)he clicks on the button so I added the ":class=>"enquirySection"" and "div class="enquirySection"". It doesn't work.
I tried something similar to that but I think it does not related to my case.
Any clue of how is this done in Rails with button_to?
Thank you.
You don't need a class, but an id
try
<%= button_to 'Make a detailed enquiry', user_path(#user, anchor: :enquirySection), :method => :get %>
or simply
<%= link_to 'Make a detailed enquiry', user_path(#user, anchor: 'enquirySection) %>
and then
<div id="enquirySection">
<%= render "enquiries/form" %>
</div>
would do the job.
I have a question with turning this html into erb.
<button name="support" type="button" value="Get Support" class="brightOrange">
<span>Get Support</span>
</button>
I've been trying to do something like this in rails.
<% form_tag get_support_path do %>
<%= text_field_tag :email, "Email:" %>
<% submit_tag "Join", :class=>"brightOrange" %>
<% end %>
Thanks for the help in advance. Not quite sure how to do this.
Try this:
button_to "Get Support", {:action => 'support/get'}.
you can give HTML options also.
See following links for more details
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
http://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to
http://snipplr.com/view/866/ruby-on-rails-form-helper-with-image-submit-button/
http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails