Button Groups for Devise /shared/_links.html.erb - ruby-on-rails

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.

Related

Attempt to Use Bootstrap on Devise

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.

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

Rails link_to issues

I am trying to make my rails application generate a link without the text and custom content instead. I am currently doing this
<%= link_to '', {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Which generates this code
<a action="show" controller="project" href>
<div class="image-container">
<img alt="House 2" src="/uploads/project_image/image/1/house-2.jpg" title="Ygfaweg">
</div>
<p>
<span class="title">
</span>
<span class="summary">
</span>
</p>
</a>
The link is incorrect and is missing alot of data, any idea why?
Remove '' from link_to method.
Change :controller in link_to method to projects
Remove braces from :id => project.id, :class => "projects"
So, refactor the first line to:
<%= link_to {:controller => :projects, :action => :show}, :id => project.id, :class => "projects" do %>
I didnt come to a pure conclusion with this issue. I ended up doing this which works fine.
<a class="project" href="<%= url_for({:controller => :project, :action => :show, :id => project.id}) %>">
Cheers everyone!
Try to do like this :
<%= link_to ({:controller => :project, :action => :show,:id => project.id}, {:class => "projects"}) do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Hope this will help you.
Just remove blank string from link_to and you are done.
<%= link_to {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>

How to add a method to link_to do ViewHelper in Rails

Thats the original Devise logout link which works:
link_to("Logout", destroy_user_session_path, :method => :delete)
Because of the and the I had to switch to this kind of link. The problem now is the link try to find the user sign_out
Tried:
<%= link_to (destroy_user_session_path, :method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, :method = 'delete') do %>
<i class="icon-off"></i> Logout</span>
<% end %>
Any ideas or suggestions?
I would do,
<span>
<i class="icon-off">
</i>
<%= link_to("Logout",destroy_user_session_path, :method => :delete)%>
</span>

Rails form submit problem having 2 forms

I have that problem it is always the same form that gets submitted.
The update_limits action gets called on the update order submit button. Which should trigger the action update_order.
Here is my view:
<h2>Movies</h2>
<h3>List movies</h3>
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) %>
<%= link_to 'create new movie', {:action => 'create',:id => params[:id]}, {:class => 'margin-left'} %>
<div class="spacer"> </div>
Number of movies in reel:
<span class="c1">
<% rr = 1..6 %>
<%= select("limits", "reel_limit", rr) %>
</span>
Number of movies in archive:
<span class="c1">
<% rr = 0..12 %>
<%= select("limits", "archive_limit", rr) %>
</span>
<%= submit_tag %>
<div class="spacer"> </div>
<%= form_tag(:controller => 'admin/photographers', :action => 'update_order' ,:id => params[:id]) %>
<ul id='movielist'>
<span class="header">name</span>
<%
n = 0
while n < #items.length
%>
<li itemID='<%=#items[n].id%>' <%= reel_color_class(n, #limits) %>>
<% if #items[n]["image"] %>
<%= image_tag("/photographer/image/#{#items[n].id}/#{#items[n]["image"]}", :size => "36x20" ) %>
<% end %>
<%=#items[n].name.force_encoding("UTF-8") %>
<span class='col2'>
<%= link_to 'edit', {:action => "edit", :id => #items[n].id} %>
<%= link_to("remove", {:action => "remove", :id => #items[n].id },
{:confirm => "Are your sure?"}) %>
</span>
</li>
<%
n = n + 1
end
%>
</ul>
<input type="hidden" name="neworder" id="neworder" value="" />
<input name="commit" type="submit" value="update order" onclick="neworder.value=(junkdrawer.inspectListOrderNew('movielist'))" />
<div class="spacer"> </div>
The form_tag method takes a block, and you are not giving it one. You should be doing something like this:
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) do %>
# form goes here
<% end %>
Or even better, if this is acting on a real object, using a form_for tag:
<%= form_for(#object) do |f| %>
# form_goes here
<% end %>
For more information, please read the Getting Started guide for Rails.

Resources