link_to with dynamic :class style in rails? - ruby-on-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' %>

Related

using link_to inside if

I have a user_authenticate method which is setting the #current_user based on session. I have a navbar layout which contains the navigation links. I want to hide one of the links and show it only if user id is 8 (which is admin). I have written the layout like this.
<div align="center" style="width: 90%">
<nav class="navbar navbar-inverse" role="navigation">
<%= link_to "Home", '/', :class => "btn btn-default navbar-btn"%>
<%= link_to "Currency Master", currencies_url, :class => "btn btn-default navbar-btn"%>
<%= link_to "Receipts", receipts_url, :class => "btn btn-default navbar-btn"%>
<%= link_to "Expenses", expenses_url, :class => "btn btn-default navbar-btn"%>
<%= link_to "Exchange", exchanges_url, :class => "btn btn-default navbar-btn"%>
<% if #current_user.id == 8 %><%= link_to "Report", reports_url, :class => "btn btn-default navbar-btn" %>
<% end %>
</nav>
The code works fine that it shows the "Report"button on navbar only of the user id is 8. But as soon as I click on the link I get following error.
undefined method `id' for nil:NilClass
Extracted source (around line #8):
6
7
8
9
10
<%= link_to "Expenses", expenses_url, :class => "btn btn-default navbar-btn"%>
<%= link_to "Exchange", exchanges_url, :class => "btn btn-default navbar-btn"%>
<% if #current_user.id == 8 %><%= link_to "Report", reports_url, :class => "btn btn-default navbar-btn" %>
<% end %>
</nav>
First you should not check if the user is an Admin by ID. You can have an "is_admin" column of boolean type and check like this:
if #current_user.is_admin?
# ...
Second, before check the value of a column, you can check first if the object itself exist. So your if condition can be like this:
if #current_user && #curent_user.is_admin?
# show admin menu
else
# show other menus
end
You are checking #current_user.id even if #current_user is not present. so it is giving an error.
First check the presence of current_user and then check id,
#current_user.present? && #current_user.id == 8
Change this line,
<% if #current_user.id == 8 %><%= link_to "Report", reports_url, :class => "btn btn-default navbar-btn" %>
<% end %>
to,
<% if (#current_user.present? && #current_user.id == 8) %><%= link_to "Report", reports_url, :class => "btn btn-default navbar-btn" %>
<% end %>

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

Adding a class to link_to block

I have a following code which displays a 'delete' link:
<%= link_to :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
But for some reason ROR is not adding some_class to a tag. Have you any idea what can i do to fix it ? Thanks in advance.
You need to add the URL as the first parameter, then the html options, e.g.:
<%= link_to resource_path(#resource), :class => 'some_class', :method => :delete, :data => { :confirm => 'Are you sure?' } do
<span>Delete</span>
<% end %>
I actually found this to be a working solution with Rails 4.2
<%= link_to(resource_path(#resource), class: "project-card clearfix") do %>
<h1>Your html here</h1>
<% end %>
If you need to pass a controller and action, like edit and destroy, do it as follow:
<%= link_to url_for(controller: controller_name, action: :edit, id: item.id), class: "btn btn-link btn-warning btn-just-icon edit" do %>
<i class="material-icons">edit</i>
<% end %>
<%= link_to url_for(controller: controller_name, action: :destroy, id: item.id), method: :delete, data: { confirm: t('common.confirm') }, class: 'btn btn-link btn-danger btn-just-icon remove' do %>
<i class="material-icons">close</i>
<% end %>
The link_to docs:
link_to(body, url, html_options = {})
So you'd want
<%= link_to <span>Delete</span>, '/someurl', :class=>'some_class', :method=>:delete, .... %>

Adding controls inline with simple_form, nested_form and Twitter Bootstrap in Rails

I'm using simple_form, nested_form and Twitter Bootstrap and trying to put the "Remove Link" from nested_form on the same line as the object.
Right now it looks like this:
http://grab.by/eKDS
and I want it to look like this:
http://grab.by/eKEc
Here's what my code looks like:
<%= cform.simple_fields_for :licensings do |lf| %>
<%= lf.input :state, :collection => us_states, :wrapper => false %>
<%= lf.link_to_remove "Remove this Licensing", :class => 'btn btn-mini btn-danger' %>
<% end %>
I've tried putting the second link_to_remove inside a block for the first lf.input but then the actual dropdown doesn't appear. I've looked through simple_form's code but I wasn't able to track down if there was a way to accomplish this.
Thanks for the answers but I couldn't get either to work. I found the answer on the Google Groups mailing list:
https://groups.google.com/forum/?fromgroups#!topic/plataformatec-simpleform/hL9ek5svyAU
<%= cform.simple_fields_for :licensings do |lf| %>
<%= lf.input :state do %>
<%= lf.input_field :state, :collection => us_states %>
<%= lf.link_to_remove "Remove this Licensing", :class => 'btn btn-mini btn-danger' %>
<% end %>
<% end %>
Have you tried to add the class "inline" to your nested form?
<%= form_for #test, :html => { :class => 'form-inline' } do |f| %>
<%= f.text_field :some_field, :class => 'text_field' %>
<%= f.submit "Save", :class => 'btn btn-primary' %>
<% end %>
As you can see in the documentation, you can create your custom wrapper. You must to add something like this in you simple_form's initializer :
config.wrappers :inline do |b|
b.use :placeholder
b.use :label_input
end
And use it like this :
<%= cform.simple_fields_for :licensings do |lf| %>
<%= lf.input :state, :collection => us_states, :wrapper => inline %>
<%= lf.link_to_remove "Remove this Licensing", :class => 'btn btn-mini btn-danger' %>
<% end %>

button_tag like link_to rails 3

I need use this code:
<%= button_tag :class => "btn btn-primary" do %>
<%= t('.follow_all')%>
<% end %>
html output:
<button class="btn btn-primary" name="button" type="submit">
Follow all
</button>
if it's possible, How can I use this button like a link, something like:
<%= button_tag new_user_registration_path, :class => "btn btn-primary" do %>
<%= t('.follow_all')%>
<% end %>
I need use button_tag helper. I can not use link_to helper, or instead, if it's possible, How can I send params from button without use a form?
What about this:
<%= button_tag(:type => 'button') do %>
<% link_to t('.follow_all'), new_user_registration_path %>
<% end %>
Use button_to: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
Try this:
<%= button_tag onclick: "location.href='{new_user_registration_path}'",
type: :button, class: "btn btn-primary" do %>
<%= t('.follow_all')%>
<% end %>

Resources