using link_to inside if - ruby-on-rails

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

Related

Button Groups for Devise /shared/_links.html.erb

I have devise forms using bootstrap.
I like to get the buttons on shared/_links.html.erb to group together.
<div class="button-group btn-group-xs" role="group">
<%- if controller_name != 'sessions' %>
<%= button_to "Log in", new_session_path(resource_name), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= button_to "Sign up", new_registration_path(resource_name), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= button_to "Forgot your password?", new_password_path(resource_name), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<%= button_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
<%= button_to "Didn't receive unlock instructions?", new_unlock_path(resource_name), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= button_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider), :class => 'btn btn-default', :type => 'button', :method => :get %>
<% end -%>
<% end -%>
</div>
I get this as a result.
I suspect it's because every button has a form tag. Is there a way I can fix this using only bootstrap?
<div class="col-md-12 control">
<div style="border-top: 1px solid#888; padding-top:15px; font-size:85%" >
<div class="button-group btn-group-xs" role="group">
<form class="button_to" method="get" action="/users/sign_up"><input class="btn btn-default" type="submit" value="Sign up" /></form>
<form class="button_to" method="get" action="/users/password/new"><input class="btn btn-default" type="submit" value="Forgot your password?" /></form>
<form class="button_to" method="get" action="/users/confirmation/new"><input class="btn btn-default" type="submit" value="Didn't receive confirmation instructions?" /></form>
</div>
</div>
</div>
Reverted back to link_to
Removed
button_to
method="get"
Reverted Back
link_to
Rails adds form tags around the button_to fields causing alignment problems, by adding btn class to link_to it did show up as a button.

How to create a rails button with an icon and text?

How would you create this button using a rail helper?
<button type="submit" class="btn">
Create My Account <i class="fa"></i>
</button>
So far I have
<%= f.submit "Create My Account", :class => "btn" %>
You can use the button form helper and pass it a block.
<%= f.button class: 'btn' do %>
Create My Account <i class="fa"></i>
<% end %>
See more at http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-button
https://github.com/bokmann/font-awesome-rails
Here you will get answer of your quesiton.
<%=f.submit (fa_icon "camera-retro", text: "Take a photo"), class: "btn" %>
Does this work?
<%= f.submit "Create My Account <i class=\"fa\"></i>", :class => "btn" %>
there are many options:-
<%= f.submit (‘<i class=“fa fa-thumbs-up fa-lg”> </i>’).html_safe,:class=>"btn "%>
--------------------------------------------------------------------
<%= button_to update_users_path, {remote: true, class: "btn btn-small"} do %>
Submit <i class="fa-save fa-md"></i>
<% end %>
--------------------------------------------------------
<%= button_tag( :class => "button_black") do %>
Submit Details <i class="fa fa-reset fa-md"></i>
<% end %>
5 different ways of doing it have already been given. I prefer:
<%= f.submit :class => "btn" do %>
Create My Account <i class="fa"></i>"
<% end %>
I think its the cleanest.

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

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

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