How do I generate a <button> tag with an <i> inside? - ruby-on-rails

I want to produce this HTML:
<a class="action-btn btn-delete" href="/product/9">
<i class="icon-trash icon-white"></i>
</a>
But I read that I need to use the button_to helper method, rather than link_to.
So I tried this:
<%= button_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
But that doesn't work.
Thoughts?

I think you can use the link_to:
<%= link_to product, :method => "DELETE", :class => "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>

I don't see how you can't use [link_to][1]
<%= link_to product, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>

Try, it may work,
<%= link_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i> Delete
<% end %>

Related

How to add glyphicon to link_to

I am trying to replace the word "like" with a heart icon.
Basically, I am trying to add <i class="glyphicon glyphicon-heart"></i> to the following:
<%= link_to 'Like', like_post_path(post), method: :post %>
Try something like
<%= link_to like_post_path(post), method: :post do %>
<i class="glyphicon glyphicon-heart"></i>
<% end %>
This is for you
<%= link_to '<i class="glyphicon glyphicon-heart"></i> Like'.html_safe, like_post_path(post), method: :post %>

Nested html with content_tag

I want to get this html using content_tag or content_tag_for:
<a href="javascript:;" class="nav-link ">
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
</a>
I could put fa_icon inside link_to, but don't know how to put span. And if it is not doable, what if i do this way:
<a href="<%=courses_path%>" class="nav-link ">
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
</a>
Why not the following?:
<%= link_to "javascript:;", class: "nav-link" do %>
<%= fa_icon 'book', 'aria-hidden': true %>
<%= content_tag :span, "Courses", class: "title" %>
<% end %>
I would try link_to ... do. Because it's a standard approach that's more descriptive than content_tag I think it will be easier to understand the original intent of the code when you come back 2 years from now to do maintenance.
Here's what your snippet might look like, to get you going:
<%= link_to courses_path, class: 'nav-link' do %>
<%= fa_icon 'book', 'aria-hidden': true %>
<span class="title">Courses</span>
<% end %>
Here is the documentation for Rails 4.2: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to

NoMethodError: undefined method `stringify_keys' for "/auctions/2/comments/9":String Rails [duplicate]

I have this edit button:
<%= link_to edit_income_path(trans), class: "btn btn-default" do %>
<i class="glyphicon glyphicon-pencil"></i>
<% end %>
And I want a delete button to look the same with this icon: glyphicon glyphicon-trash, but I can't find the right syntax to make it work and look the same. My delete button now:
<%= button_to "delete", trans, :method=> :delete, :remote=> true %>
Is this what you are looking for??
<%= button_to trans, method: :delete, remote: true, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
try this...for example with link_to
<%= link_to (‘<i class=“fa fa-thumbs-up fa-lg”> </i>’).html_safe, vote_path(#image), :method=> :delete, :remote=> true%>

How to DRYing action buttons in Rails 4?

I have a dozen different Views/Controllers for administrator. In every view I have got something like this:
<%= link_to edit_topic_path(topic) do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to topic, method: :delete, confirm: "Are you sure?" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
Now my question is, How can I DRY this so I just pass some vars or object to a method and generate the above code for different views.
Thank you.
You should crate a partial
_controls.html.erb
<%= link_to path do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %> |
<%= link_to model, method: :delete, confirm: "Are you sure?" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
Then you can call it as follows:
<%= render :partial => "controls", :locals => {:path => edit_topic_path(topic), :model => topic} %>

How do I do an <i> tag within a link_to helper?

I would like to replicate this:
<i class="icon-pencil"></i>Take The Pledge
I tried this:
<%= link_to("Take the pledge", root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil"></i>
<% end %>
But it gave me this error:
NoMethodError at /
undefined method `stringify_keys' for "/":String
At the link_to call.
Thoughts?
According to the documentation you need to put the (complete) link text in the block, like this:
<%= link_to(root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil">Take the pledge</i>
<% end %>
<%= link_to(root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil"></i>
Take The Pledge
<% end %>

Resources