link_to with image, text and css in rails - ruby-on-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 %>

Related

Rails - Link a thumbnail image to a larger full image

I'm new to Rails and I'm trying to decipher how to link an image to open a different image (thumbnail vs large image, and they aren't the same).
I've tried:
<%= link_to "fullNews.jpg" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
As well as trying to use erb inside of erb:
<%= link_to "<% image_tag("fullNews.jpg") %>" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
Even a direct link:
<%= link_to "assets/images/fullNews.jpg" do %>
<% image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>
I'm sure it's just a syntax thing but I can't figure out how to achieve this?
Pretty easy:
<%= link_to image_url("fullNews.jpg") do %>
<%= image_tag("smallNews.jpg", :alt => "Newsletter Preview", class: "grow shadow") %>
<% end %>

link_to with dynamic :class style in rails?

How would one structure a link_to in rails based enabling and disabling it with a class, so for example a disable link would look like this;
<%= link_to 'something', :somewhere, :class => 'btn btn-primary disabled' %>
but enabled would look like this
<%= link_to 'something', :somewhere, :class => 'btn btn-primary' %>
Now rather then doing this;
<% if current_user.is_admin? %>
<%= link_to 'something', :somewhere, :class => 'btn btn-primary' %>
<% else %>
<%= link_to 'something', :somewhere, :class => 'btn btn-primary disabled' %>
<% end %>
is there a cleaner and simple way of doing it ?
you can do this in two ways:
string interpolation
<%= link_to 'something', :somewhere, :class => "btn btn-primary #{current_user.is_admin? ? 'enabled' : 'disabled'}" %>
link_to_if
now this doesn't really deal with the class but it makes the link a simple text if the condition is not satisfied
<%= link_to_if current_user.is_admin?, 'something', :somewhere, :class => 'btn btn-primary' %>

adding a css inside a link_to do function

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

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

Adding an ID or Class in Ruby on Rails?

I've got the following code for a search form, but how would I add an ID or a class to the submit button?
<% form_tag '/wine/search/', :method => 'get' do %>
<%= label_tag "Search" %>
<%= text_field_tag :search_string, params[:search_string] %>
<%= submit_tag "Go" %>
<% end %>
Thanks
submit_tag "Go", :class => "some_class"
submit_tag "Go", :id=> "some_id"
From the Rails API FormTagHelper

Resources