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' %>
Related
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" %>
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...' } %>
I'm new to Ruby and programming in general. I'm creating this log in page using simple form but the Create account button is to close to the confirm password form I don't know how to enter a break between these two.
Thank you!
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %>
<%= devise_error_messages! %>
<%= f.input :email, :label => 'Correo Electronico' %>
<%= f.input :password, :label => 'Contraseña' %>
<%= f.input :password_confirmation, :label => 'Confirmar Contraseña' %>
<div>
<%= f.submit "Crear cuenta", class: "btn btn-primary" %>
</div>
<% end %>
You should use a HTML class/id and put these rules in a CSS file. But there is 2 ways:
The quick & diry way:
<div style="margin-top: 15px;">
<%= f.submit "Crear cuenta", class: "btn btn-primary" %>
</div>
The proper way:
# app/assets/stylesheets/application.css.scss (or extension can be .sass)
body div.form_action_buttons {
margin-top: 15px;
}
# your view
<div class='form_action_buttons'>
<%= f.submit "Crear cuenta", class: "btn btn-primary" %>
</div>
I'm trying to embed additional code in a link_to but not sure how to get it to work. How do I go about doing that? I'm trying to get this to work:
<%= link_to image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") + <%= activity.votes.size %>, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>
I would try to use linkt_to as block to gain readability
<%= link_to favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" do %>
<%= image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") %>
<%= activity.votes.size %>,
<% end %>
I hope that the above code works for you.
you can do it this way also.
<%= link_to "#{image_tag('Favorite 3.png', class: 'act_actions', title: 'Unfavorite', alt: 'Unfavorite')} #{activity.votes.size}".html_safe, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>