Nested html with content_tag - ruby-on-rails

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

Related

How to add a facebook glyphicon to rails link_to

I am trying to add a glyph-icon to my Facebook link_to in ruby on rails but when I try it doesn't show up any idea on how to do this?
This is the what I have attempted:
<li>
<i class= "fa fa-facebook"></i>
<%= link_to "Facebook", '/facebook' %>
</li>
any help is greatly appreciated..thank you
<%= link_to '/facebook' do %>
<i class= "fa fa-facebook"></i>
<% end %>
what about:
<li>
<%= link_to '/facebook' do %>
Facebook // delete if you don't need text
<i class= "fa fa-facebook"></i>
<% end %>
</li>

How do I output the contents of a variable in grid form?

I have the following code for my Ruby on Rails project, which outputs a list of users on a page:
<ul class="users">
<%= render #users %>
</ul>`
The result is a long list of users on the page.
My question is: How can I manipulate #users so that I can output its content in grid form (e.g., a row of 5 users and then next row and then next and so on) on the page? Thanks.
This is a really simple example and you can customise into a nicer table.
The basic idea is to group the array of users in subarrays of n elements, lets say 5.
<% #users.each_slice(5).each do |row| %>
<p><% row.each do |user| %>
<%= user.name %> |
<% end %>
</p>
<% end %>
<div class="row">
<% users.in_groups_of(5) do |group| %>
<% group.compact.each do |user| %>
<div class="col-lg-3 col-md-4 col-xs-6 thumb">
<%= image_tag (user.file_url), class: "img-sz" %>
<h4><%= link_to user.title, user %></h4>
<%= link_to "Edit", edit_user_path(user) , remote: true, class: "btn btn-default btn-xs" %>
<%= link_to "delete", user,remote: true, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-xs" %></br></br>
</div>
<% end %>
<% end %>
<% end %>
</div>
This is example code change the name of attributes according to your structure . This will help you in solving your problem .

undefined method `stringify_keys' for "/accounts/15":String

I am trying to display a list of accounts, i am getting the above error in my link_to method. Below is my link_to method.
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account.name, account_path( account.accountid ),
class: "list-group-item active" do %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>
If you're using the block form of link_to you can't have text content (the block is your text content). If you look at rails helpers. The syntax for block form looks like:
link_to(url, html_options = {}) do
# name
end
So you need to do something like:
<div class="list-group">
<% #accounts.each do |account| %>
<%= link_to account_path( account.accountid ), class: "list-group-item active" do %>
<%= account.name %>
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="badge">44</span>
<% end %>
<% end %>
</div>

How do I generate a <button> tag with an <i> inside?

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 %>

how to convert this ruby on rails code to include html as well

How can I take this..
<li>
<%= link_to image_tag(product.photo.url(:small)), product_path(product.id, :forum_id => params[:forum_id]) %>
<% if params[:forum_id] %>
<%= link_to "Add", addtoforum_user_path(:products => [product.id],:forum_id => params[:forum_id], tp: "add") %>
</li>
to be like this.. (right now it is only inlcluding the image)
<li>
<a href="">
<figure>
<img src="image-url....">
</figure>
<span>product name</span>
</a>
</li>
Thanks
Not sure I understand the question but I guess the issue is to output the figure, img tags etc inside the a tag?
This can be achieved by passing a block to the link_to method, like this:
<li>
<%= link_to product_path(product.id, :forum_id => params[:forum_id]) do %>
<figure>
<%= image_tag(product.photo.url(:small)) %>
</figure>
<span><%= product.name %></span>
<% end %>
</li>

Resources