html_safe not working with rails - ruby-on-rails

I can't manage to make it work... even using raw or html_safe
view
<%= button_to(glyphicon('heart', 'I love it !'), some_path, class: "btn btn-success")%>
helper
def glyphicon(glyph, text = nil)
html = "<i class=\"glyphicon glyphicon-#{glyph}\"></i>"
html += " #{text}" if text
html.html_safe
end
result
A success btn with <i class= and " /> after
The following works (I've been doing it for ages), but the extra do syntax is annoying...
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
<i class="glyphicon glyphicon-heart"></i> I love it !
<% end %>
EDIT
Found a more compact syntax :
<%= button_to(some_path(#etude), class: "btn btn-success"){
glyphicon('heart', 'I love it')} %>

I don't think its a problem with html safe, rather the name attribute for button_to will only accept plain text.
<%= button_to("<b>hello</b>".html_safe, 'http://www.google.com', :class => 'button', :method => 'get') %>
and
<%= button_to("<b>hello</b>", 'http://www.google.com', :class => 'button', :method => 'get') %>
both produce a button with <b>hello</b> written on it rather than just hello in bold.
The method using do is the only way to do it, it's the same thing you need to do when linking glyphicons via link_to.
But you could do this.
<%= button_to(some_path(#etude), class: "btn btn-success") do %>
glyphicon('heart','I love it')
<% end %>

Related

Outputting a Rails 5.0 Key Value Path as a String

I'm very new to Rails so I'll try and explain this the best I can. I have a simple Products model with one of the fields being the name of the Stripe plan name. This is so a new product can be added and the selection will append the plan name to the end of the path. It works great hard coded for testing.
Heres my products/show.html.erb line in questions. I am only including this for now as any experienced Rails developer should see the issue.
products/show.html.erb hard coded
...
<%= link_to 'Subscribe', new_subscription_path(plan: 'monthly'), class: 'btn btn-primary' %>
...
products/show.html.erb what I need
...
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
...
Having <%= #product.stripe_name %> in as a string throws an error.
The desired outcome would look like this in the browser
http://127.0.0.1:3000/subscription/new?plan=monthly
This:
<%= link_to 'Subscribe', new_subscription_path(plan: '<%= #product.stripe_name %>' ), class: 'btn btn-primary' %>
Should be this:
<%= link_to 'Subscribe', new_subscription_path(plan: #product.stripe_name), class: 'btn btn-primary' %>
You don't put <%= ... %> inside another <%= ... %>.

Adding icon in data-loading-text in Rails submit_tag

I have the following code:
<%= submit_tag t('submit'), class: "btn btn-primary", id: "email_submitbutton", "data-loading-text" => "<i class='fa fa-circle-o-notch fa-spin'></i> Processing Order" %>
The icon tag in data-loading-text is processed as pure text by Rails.
I have tried using data {...} to wrap around it, but no change
I think what you're looking for is the disable_with option and using a button.
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/submit_tag
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/button_tag
The reason for html not working in a submit tags is that they're turned into HTML input tag with the type submit.
<%= submit_tag "<p>Hi</p>" %>
<input type="submit" value="<p>Hi</p>">
Rails 3 Submit Tag + html_safe
<%= button_tag t('submit'), class: "btn btn-primary", id: "email_submitbutton", data: { disable_with: "<i class='fa fa-circle-o-notch fa-spin'></i> Processing Order".html_safe } %>
Also, I highly suggesting using a Font-Awesome gem.
https://github.com/FortAwesome/font-awesome-sass
This is cool because it means you don't need to hand write HTML in you're Ruby code.
I found an alternative way to achieve the same effect as Bootstrap's data-loading-text (changing text in button when clicked).
I changed data-loading-text to disable_with and change submit_tag with button_tag:
<%= button_tag t('submit'), class: "btn btn-primary", id: "email_submitbutton", data: {disable_with: "<i class='fa fa-circle-o-notch fa-spin'></i> Processing Order".html_safe} %>
It works as expected

Adding a span on Rails 3 - Where I have to add the span?

I am pretty new on Rails and Ruby and I cannot find the answer to add a span inside this link_to
<p class="withButtons" >
<%= link_to 'RĂ©pondre', lawyer_answers_path(#current_lawyer ? 'mon-compte' : ''), :class => 'button answerRevealer reply' %>
</p>
You can pass a block to link_to
<%= link_to lawyer_answers_path(#current_lawyer ? 'mon-compte' : ''), :class => 'button answerRevealer reply' do %>
<span>RĂ©pondre</span>
<% end %>

Rails: link_to not rendering dynamic text

In my rails 4 erb view i have this link,
<%= link_to "Download #served_file.title", "/public/uploads/#served_file.file", :class => "btn" %>
However, it won't replace #served_object.title and #served_object.file with the text but just placing those lines inside a <%= %> works as expected.
You need to interpolate the values into the string.
<%= link_to "Download #{#served_file.title}", "/uploads/#{#served_file.file}", :class => "btn" %>

button_tag not send commit

I need use button_tag instead submit_tag on a form to add icon with style http://twitter.github.com/bootstrap/base-css.html#buttons.
<%= simple_form_for(bla..........bla......)) do %>
<%= button_tag t('.sent_to_trash'), :class => "btn btn-small btn-primary disabled", :id => "trash_button", do %>
<i class="icon-trash icon-white"></i>
<%= t('.sent_to_trash') %>
<% end %>
<% end %>
The question is I can not receive the params[:commit] with button_tag, however with submit_tag is working fine and I receive correctly params[:commit] on my action controller.
How can I fix this problem?
Thank you very much!
Html element button works with :name and :value params, so you have to explicitly define these, e.g.
= button_tag(:name => "commit", :value => "my_button") do
= "Press me!"
then you get params[:commit] = "my_button" after form submit.
Note: You should specify :type attribute too, because different browsers use different default types for the <button> element (:type => "submit")

Resources