Rails Error - undefined local variable or method `signout_path' - ruby-on-rails

The error is from app/views/layouts/_header.html.erb. That piece of line is part of my app/views/layouts/_header.html.erb whic is the following:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>

Ok, it's getting late here and i don't wait for your output of rake routes or routes.rb file.
As mentioned in my comment, got to your shell, cd to your application directory and run
rake routes
this will display all possible routes. Chances are, that there's already a route for the sign out process.
If not, you have to create the route in your /config/routes.rb and direct it to the right controller. For example
delete '/logout', to: 'sessions#destroy', as: 'signout'
where 'delete' is the request, '/logout' my example url, 'sessions' the controller and 'destroy' it's action and 'signout' your method name (as you wished it in your code).
you can just write
delete '/logout', to: 'sessions#destroy'
in this case, you have to replace your method-name signout_path for example with the url.
One last hint: Logging out can be done, too with GET instead of DELETE. Maybe you created a route with only the GET method? Should be obvious in the output of rake routes, too.

Related

Where should I set the current_user so I don't get a missing key error?

My question assumes a specific answer but it may not but accurate.
I've created a route:
get 'users/:id/groups/' => 'users#groups', as: "my_groups"
but when I go to access this path, conditionally, in a view, i get an exception:
ActionController::UrlGenerationError at /
No route matches {:controller=>"users", :action=>"groups"} missing required keys: [:id]
Here is the partial with the offending line:
<% if user_signed_in? %>
<li id="groups-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Groups <b class="caret"></b>
</a>
<ul class="dropdown-menu">
#offending line:
<li><%= link_to 'My Groups', my_groups_path %></li>
<li><%= link_to 'Create a Group', new_group_path %></li>
</ul>
</li>
<li><%= link_to 'Logout', destroy_user_session_path, :method=>'delete' %></li>
<% else %>
<li><%= link_to 'Login', new_user_session_path %></li>
<% end %>
<% if user_signed_in? %>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<% end %>
<% if user_signed_in? %>
<% if current_user.has_role? :admin %>
<li><%= link_to 'Admin', users_path %></li>
<% end %>
<% end %>
it seems that there is no current_user set, perhaps?
It's unclear how this could break if the user_signed_in? method is working.
You need to pass in the :id parameter to the url helper function
<li><%= link_to 'My Groups', my_groups_path(current_user) %></li>

No method error after adding a some HTML with embedded ruby

The code below works as it should, but after adding a dropdown navigation with some embedded ruby, I get a no method error.
NoMethodError in Static_pages#home
Showing D:/Ruby/sample_app/app/views/layouts/_header.html.erb where line #9 raised:
undefined method `find_by_token' for #Class:0x467a948
My _header.html.erb which gives no errors
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Energy Battle", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Log in", signin_path %></li>
</ul>
</nav>
</div>
</div>
</header>
This is the _header.html.erb with the code I added. Which gives me the error.
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Energy Battle", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
It's telling you that there is a problem with your signed_in? method.
That's where you need to start checking.
Hope this helps

Why does my Home link try to take me to '/root_path' instead of '/'?

I am working through this tutorial -> http://ruby.railstutorial.org/chapters/filling-in-the-layout
You can experience the problem here -> intense-dusk-3202.herokuapp.com/
When I click on the "Home" link in the header it tries to take me to http://intense-dusk-3202.herokuapp.com/root_path instead of just http://intense-dusk-3202.herokuapp.com/
Here is my routes.rb
SampleApp::Application.routes.draw do
get "users/new"
root :to => 'static_pages#home'
match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
Here is my app/views/layouts/_header.html.erb file:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", 'root_path', id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", 'root_path' %></li>
<li><%= link_to "Help", 'help_path' %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
</div>
</header>
Do you need to see anything else before you can help me?
You shouldn't quote those helpers:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Sign in", '#' %></li>
</ul>
</nav>
</div>
</div>
</header>
root_path, help_path, and all other named route helpers are actually ruby methods. Just call them directly, don't put quotes around them.

Showing users name on nav bar when logged in using ruby on rails

I want to be able to replace Accounts tab on my navigation bar with the User's name when they logged in. How would i go about doing that. Would i have to use some sort of embedded ruby like <%= #user.name %>
heres the html
enter code here
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", user_path(current_user) %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: 'delete' %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
Try:
<%= current_user.name %>

_header.html.erb account dropdown function does not work

I am following the ruby on rails tutorials from http://ruby.railstutorial.org .
The following is the _header.html.erb code, is the same as the tutorials. but the drop down function of account does not work.
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
Without seeing more of the application it's hard to determine the exact problem, however based on what you have supplied I can suggest the following:
refer to http://twitter.github.com/bootstrap/javascript.html#dropdowns and ensure you are using the right markup nesting (looks like you are)
make sure you have included the javascript files within your application.js file (your manifest file). The docs for bootstrap here: http://twitter.github.com/bootstrap/components.html#navbar say that you need to have the dropdown JS file included to make the dropdown work properly.
Please let me know if any of this helps.

Resources