Attempt to Use Bootstrap on Devise - ruby-on-rails

So, I am trying to use Bootstrap to format the shared links of the Devise gem. My current iteration is the following:
<%- if controller_name != 'sessions' %>
<%= link_to "Log in" :class=>"btn btn-success btn-lg", new_session_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up" :class=>"btn btn-info btn-lg", new_registration_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?" :class=>"btn btn-warning btn-lg", new_password_path(resource_name) %><br />
<% end -%>
This is located, as a partial, under Devise->Shared. There seems to be one unopened thread on here asking about this, but I couldn't find a solution and no attempt that I've made has resulted in a solution. I keep getting syntax errors no matter what I do.

Change this:
<%= link_to "Log in" :class=>"btn btn-success btn-lg",
new_session_path(resource_name) %>
to:
<%= link_to "Log in", new_session_path(resource_name), :class => "btn
btn-success btn-lg" %>
Same goes with other link_to.
See link_to docs.

Related

Chaning image-button to text-button using Devise login

I have it currently so someone can click on the login button and when somebody signs in it changes that to logout. How would I be able to make it so it sets the button as the user's avatar that they have uploaded (or default one) when they login and then when they logout it changes back to "sign in"?
<div class="dropdown">
<button class="button-login">
<% if user_signed_in? %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path %>
<% end %>
</button>
<div class="dropdown-content">
<p>
<% if user_signed_in? %>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<% end %>
</div>
I believe you can do this by using a block:
<%= link_to destroy_user_session_path, method: :delete do %>
<%= image_tag("avatar.jpg", :alt => "user avatar image") %>
<% end %>

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

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.

Best way to display something based on if user logged in or not

I have an if statement that displays certain link based if a user voted or not for something. However, I'll get an error if the user is not logged in since current_user will be nil. I'm using devise for authentication.
undefined method `voted_as_when_voted_for' for nil:NilClass
<% if current_user.voted_as_when_voted_for #pin %>
<%= link_to dislike_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span> <%= #pin.get_upvotes.size %>
<% end %>
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
<%= link_to pins_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-step-backward"></span>
Back
<% end %>
<% if user_signed_in? %>
<%= link_to "Edit", edit_pin_path, class: "btn btn-default" %>
<%= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure"}, class: "btn btn-default" %>
<% end %>
What can I add to that if statement to display one of the links even if the user is not logged in without repeating link_to code. Using DRY Rails methodology. (if a user is not logged in and clicks like/dislike link, it will ask him to log in since I have my routes set up like this:
before_action :authenticate_user!, except: [:index, :show]
Try this:
<% if current_user && current_user.voted_as_when_voted_for #pin %>
... code here
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
In your case:
<% if signed_in? && current_user.voted_as_when_voted_for #pin %>
...
<% end %>
Assuming you're using devise since you included the authenticate_user! method.

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