adding a css inside a link_to do function - ruby-on-rails

I'm trying to make link_to function have a css class attached to it,
My code is something like this:
<%= link_to newGame do%>
<%= image_tag newGame.image_url.to_s %>
<% end %>
which links an image to its content, but I need to add a class="thumbnail" to the link,
ie:
<a href="/games/:id" class="thumbnaill>
instead of what its currently generating:
<a href="/games/:id">
Thanks

Ref link_to
<%= link_to newGame, class: 'thumbnail' do %>
Be careful when using the older argument style, as an extra literal hash is needed:
<%= link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article" %>
#Gives Articles

You can just do
<%= link_to newGame, class: 'thumbnail' do %>
<%= image_tag newGame.image_url.to_s %>
<% end %>

Related

How to route actions based on object class with namespaced controllers in Rails 5.2?

I am refactoring my application so that users are now managed in a dedicated Administration namespace. I did the following:
routes.rb
namespace :administration do
get 'users', to: "/administration/users#index"
resources :users
resources :groups
end
The Users and Groups controller are now in the app/controllers/administration folder. They are defined as Administration::UsersController and Administration::GroupsController. The models are not namespaced.
The related links are now based on administration_users_path and administration_groups_path.
I use some partials for hamonised layout. One of them diplays Edit and Delete buttons, where this_object represents the instance passed to the locals:
_object_actions.erb
<% if this_object.is_active %>
<%= link_to [ :edit, this_object], method: :get, class: "mat-flat-button mat-button-base mat-primary" do%>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to this_object, data: { confirm: t("Sure") }, method: :delete, class: "mat-flat-button mat-button-base mat-warn" do%>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, this_object], method: :post, class: "mat-stroked-button mat-button-base" do%>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
But coming to to Users and Groups, the path to actions is not properly built. I would need to build the URL such as /administration/users/1/edit, but it actually is /users/1/edit.
How can I make sure to build the correct URL for this purpose?
Thanks to this post Rails using link to with namespaced routes, I undestood that it is possible to add more parameters to the link_to method to build the actual link.
To call namespaced controller methods, I added the following to the links:
<%# Check if the parent is a module defined as Namespace. If not (Object) then display default link %>
<% namespace = controller.class.parent.name == 'Object' ?
nil :
controller.class.parent.name.downcase %>
<% if this_object.is_active %>
<%= link_to [ :edit, namespace, this_object],
method: :get,
class: "mat-flat-button mat-button-base mat-primary" do %>
<span class="fa fa-edit"></span>
<%= t("Edit") %>
<% end %>
<%= link_to [namespace, this_object],
data: { confirm: t("Sure") },
method: :delete,
class: "mat-flat-button mat-button-base mat-warn" do %>
<span class="fa fa-trash"></span>
<%= t("Destroy") %>
<% end %>
<% else %>
<%= link_to [ :activate, namespace, this_object],
method: :post,
class: "mat-stroked-button mat-button-base" do %>
<span class="fa fa-trash-restore"></span>
<%= t("Recall") %>
<% end %>
<% end %>
You can specify the desired route for each link_to
<%= link_to "Edit", edit_administration_user_path(this_object) %>

Is there anyway to make an object managed by Best_In_place also be a link?

I am using the Best In Place gem.
This is an example of my object:
<h5><%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %> <%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %></h5>
But what I want to do is have the node.name attribute be shown as a regular link.
So just link_to node.name, node.
How do I combine the two?
Use the display_with option? It takes a helper or proc. I would prefer a helper, but for brevity I show it with a proc:
<%= best_in_place node, :name, as: :input,
activator: "#edit-node-title-#{node.id}"
display_with: proc{|node| link_to node.name, node_path(node) }
%>
<%= link_to "<i class='fa fa-pencil'></i>".html_safe, "#", id: "edit-node-title-#{node.id}" %>
You could wrap your Best in Place tag inside of a link_to tag:
<h5>
<%= link_to node_path(node) do %><!-- Or whatever path you want to link_to -->
<%= best_in_place node, :name, as: :input, activator: "#edit-node-title-#{node.id}" %>
<% end %>
<%= link_to "#", id: "edit-node-title-#{node.id}" do %>
<i class='fa fa-pencil'></i>
<% end %>
</h5>
I also edited your activator link so it is a block too. This is untested, but it should work.

link_to with image, text and css in rails

I am trying to create a btn (using css) that will have both an image and a text.
I have the following code that works without the class, but when I am adding the class my code breaks. Is there a way to combine all of the above in link_to?
<%= link_to new_task_path do %>
<%= image_tag("new.png"), :class => 'btn btn-info' %> New Task
<% end %>
Solved with
<%= link_to image_tag("new.png") + "New Task" , new_task_path , :class => 'icon btn' %>
add the class on tag(anchor tag)
<%= link_to new_task_path, :class => 'btn btn-info' do %>
<%= image_tag("new.png") %> New Task
<% end %>

How to pass html for a link_to block?

I'm trying to pass a link_to block with html but can't get it. I tried some other ways with no luck so I will use my original code:
<% link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
This doesn't show the :class though.
What needs to be corrected?
Try to add = to make it <%= %>
<%= link_to survey_path(survey), :class => "button" do %>
<span>add questions to <%= survey.name %></span>
<% end %>
In the view code in Rails 3 applications it’s sometimes necessary to
use <%= instead of <% at the beginning of blocks that output content,
such as form_for.
Since it's just a span, why don't you just do
<%= link_to "add questions to #{survey.name}", survey_path(survey), :class => "button" %>

Embed additional HTML inside of link_to call

How do I embed additional HTML inside of a link_to call?
Here is my desired result:
<i class="icon-show"></i>Show
Here is my link_to call
<%= link_to "Show", exercise_path(exercise) %>
<%= link_to '<i class="icon-search"></i> Show'.html_safe, exercise_path(exercise), :class => 'btn btn-small' %>
.html_safe is required so that it is not escaped.
It is cleaner if you wrap it in a do block
<%= link_to exercise_path(exercise) do %>
<i class="icon-search"></i> Show
<% end %>

Resources