Error :undefined local variable or method `delete' in logout link - ruby-on-rails

I am creating a login-logout link. I have used my devise for user and getting the following error.
Showing /home/sushmitha/ground/remote_modals_demo-master/app/views/layouts/_header.html.erb where line #9 raised:
undefined local variable or method `delete' for #<#:0x00007f3920c61e38>
//_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<h1><%= link_to "Cricket", root_path, id: "logo" %></h1>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", '#' %></li>
<% if user_signed_in? %>
<li><%= link_to "Sign out", destroy_user_session_path, method:delete %></li>
<% else %>
<li><%= link_to "Log in", user_session_path %></li>
<% end %>
</ul>
</nav>
</div>
</header>
user routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy

It should be method: :delete.

Related

Routing error UsersController

I have a link_to that takes the users to their profile page. It used to work before but now I keep on getting a uninitialized constant UsersController.
Routes:
Rails.application.routes.draw do
resources :users
resources :questions
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
end
Index.html.erb:
<div class="row">
<div class="col-md-8">
<% if #questions.any? %>
<% #questions.each do |question| %>
<div class="well">
<div class="media">
<a class="pull-left">
<% if question.user.avatar.blank? %>
<img src="http://www.adtechnology.co.uk/images/UGM-default-user.png" style="width: 75px;">
<% else %>
<%= image_tag question.user.avatar, :style => "width:75px;" %>
<% end %>
</a>
<div class="media-body">
<h4 class="media-heading"><%= link_to question.title, question_path(question), :class => "ques" %></h4>
<p class="text-right">By <%= link_to question.user.username, question.user, :class => " bg" %></p> <!-- that's what's causing the error -->
<p class="text-muted"><%= truncate(question.description, :length => 50) %></p>
<ul class="list-inline navbar-right list-unstyled">
<li><span style="padding-right: 10px;" ><i class="fa fa-calendar"></i> asked <%= time_ago_in_words(question.created_at) %> ago </span></li>
</ul>
</div>
</div>
</div>
<% end %>
<% else %>
<p>No content yet</p>
<% end %>
Rake routes:
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
questions_path GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question_path GET /questions/new(.:format) questions#new
edit_question_path GET /questions/:id/edit(.:format) questions#edit
question_path GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
new_user_session_path GET /users/sign_in(.:format) devise/sessions#new
user_session_path POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session_path DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password_path POST /users/password(.:format) devise/passwords#create
new_user_password_path GET /users/password/new(.:format) devise/passwords#new
edit_user_password_path GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration_path GET /users/cancel(.:format) devise/registrations#cancel
user_registration_path POST /users(.:format) devise/registrations#create
new_user_registration_path GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration_path GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root_path GET / home#index
GET /users/:id(.:format) home#profile
As I said before, I don't really know what's causing the error. It was working properly yesterday and I didn't do any changes at all.
You should use <%= link_to home_profile(question.user.id) %>.
Assuming question.user.id get's user's corresponding id.
Acess users's id in Profile controller by params[:id].
I fixed it by putting the resources at the bottom instead:
Rails.application.routes.draw do
devise_for :users
root 'home#index'
get '/users/:id' => 'home#profile'
resources :users
resources :questions
end
My guess is that it was overriding the devise_for :users

Destroy User not working on Rails

I'm using devise on my Users controller right now and can't seem to destroy my users. Edit, new still work. Destroy only redirects me to showing the user attributes.
My User Controller:
class UsersController < ApplicationController
def new
#user = User.new
end
def create
#user = User.new(user_params)
if #user.save
redirect_to users_path
else
render 'new'
end
end
def index
#users=User.all
end
def edit
#user = User.find(params[:id])
end
def destroy
#user = User.find(params[:id])
#user.destroy
redirect_to users_path
end
def delete
end
def update
#user = User.find(params[:id])
if #user.update_attributes(user_params)
redirect_to user_path(#user.id)
else
render 'edit'
end
end
def show
#user = User.find(params[:id])
end
private
def user_params
params.require(:user).permit(:name, :email, :password)
end
end
User View:
<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<table>
<tr>
<td>name</td>
<td>email</td>
<td>password</td>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user_path(user.id) %></td>
<td><%= user.email %></td>
<td><%= user.password %></td>
<td><%= link_to "edit user", edit_user_path(user.id) %> </td>
<td><%= link_to "destroy user", user, method: :delete %> </td>
</tr>
<% end %>
</table>
Does it have something to do with the
<td><%= link_to "destroy user", user, method: :delete %> </td>
line? It seems like this is the line that is giving me the most trouble. I am using devise if that is relevant, and I am also having issues trying to get it fixed.
My rake routes:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
Because you're using devise, it wont work in your regular controller.
You would have to use the registration path not the user path:
<%= link_to "Delete User", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>

Rails Devise Engine Sign_out link Error

I've installed devise on my engine and applied the following in my application.html.erb file:
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. This cannot be cheese?
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
<% end %>
</div>
It shows No route matches "/users/sign_out" and it shows error for
<%= link_to 'Register', new_user_registration_path %> or <%= link_to 'Sign in', new_user_session_path %>
Error Message NameError in Devise::Sessions#new
undefined local variable or method `destroy_user_session_path' for #<#<Class:0x007fb4da2e9618>:0x007fb4da2db9a0>
Rake Routes Result
Routes for Fd::Engine:
users_auth_google_oauth2_callback GET /users/auth/google_oauth2/callback(.:format) fd/omniauth_callbacks#google_oauth2
fd_test_index GET /fd/test/index(.:format) fd/fd/test#index
fd_omniauth_callbacks_google_oauth2 GET /fd/omniauth_callbacks/google_oauth2(.:format) fd/fd/omniauth_callbacks#google_oauth2
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/google_oauth2/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) omniauth_callbacks#(?-mix:google_oauth2)
fd_auth_index GET /fd/auth/index(.:format) fd/fd/auth#index
root GET / fd/auth#index
omniauth_callbacks_verify_otp GET /omniauth_callbacks/verify_otp(.:format) fd/omniauth_callbacks#verify_otp
omniauth_callbacks_delete_session GET /omniauth_callbacks/delete_session(.:format) fd/omniauth_callbacks#sign_out
Engine routes:
Fd::Engine.routes.draw do
devise_for :users,:controllers => { :omniauth_callbacks => "omniauth_callbacks" }, :class_name => "Fd::User", module: :devise
end
You Integrated OmniAuth in Devise, so if user gives correct credentials devise will create session , From your question i understand session is successfully created so if you want clear session just create a action in a controller(where you start session) and give
redirect_to "/users/sign_out"
or
try
redirect_to destroy_user_session_path
I hope this will work
Try this :
<%= link_to "Sign out", destroy_user_session_path, :method => :get %>
And make sure that you have below line in your config/initializers/devise.rb
config.sign_out_via = :get

undefined method `username' for nil:NilClass when signing up and signing in

Whenever I try to log in and sign up, I am hit with this error:
undefined method `username' for nil:NilClass
on these two lines:
<strong><%= #user.username %></strong> <br>
<strong><%= #user.name %></strong>
Here is the full error from the server's output:
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'sign_in' LIMIT 1
Rendered users/show.html.erb within layouts/application (2.1ms)
Completed 500 Internal Server Error in 7ms
ActionView::Template::Error (undefined method `username' for nil:NilClass):
1: <strong><%= #user.username %></strong> <br>
2: <strong><%= #user.name %></strong>
3: <%= debug #user %>
4:
app/views/users/show.html.erb:1:in `_app_views_users_show_html_erb__1133891108745893964_2164088020'
Here are my routes:
Stynyl::Application.routes.draw do
resources :things
resources :users, only: [:show]
devise_for :users
get '/about', to: 'pages#about'
root 'things#index'
end
Here is my user show view
<strong><%= #user.username %></strong> <br>
<strong><%= #user.name %></strong>
<div id="things" class="transitions-enabled">
<% #user.things.each do |thing| %>
<div class='panel panel default'>
<div class="box">
<%= link_to image_tag(thing.image.url(:medium)), thing %>
<div class='panel-body'>
<strong><p><%= thing.title %></p></strong>
<p><%= thing.description %></p>
By <%= link_to thing.user.username, thing.user %>
<% if thing.user == current_user %>
<%= link_to edit_thing_path(thing) do %>
<span class='glyphicon glyphicon-edit'></span> Edit
<% end %>
<%= link_to thing_path(thing), method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class='glyphicon glyphicon-trash'></span> Delete
<% end %>
</div>
<% end %>
</div>
</div>
<% end %>
</div>
Here is my UsersController file:
class UsersController < ApplicationController
def show
#user = User.find_by_username(params[:id])
end
end
Output of rake routes:
Prefix Verb URI Pattern Controller#Action
things GET /things(.:format) things#index
POST /things(.:format) things#create
new_thing GET /things/new(.:format) things#new
edit_thing GET /things/:id/edit(.:format) things#edit
thing GET /things/:id(.:format) things#show
PATCH /things/:id(.:format) things#update
PUT /things/:id(.:format) things#update
DELETE /things/:id(.:format) things#destroy
users POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
user GET /users/:id(.:format) users#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
about GET /about(.:format) pages#about
root GET / things#index
Edits
I also noticed that when I delete all of the code from the User show view, I can get to the signup and login pages, but they are blank. Can we safely guarantee that the problem is in the view? I'm so perplexed at how something in the show view can affect the ability to sign in and sign up!
I have also observed that when I add content to the users show view, it appears on the sign up and login pages. What on earth is going on?
The reason is because of a conflict in your routes. When matching a request to a route, Rails will go through your routes sequentially. But your order shows:
user GET /users/:id(.:format) users#show
new_user_session GET /users/sign_in(.:format) devise/sessions#new
And so Rails is passing sign_in as the :id parameter to your user show method, instead of being caught by devise's new_user_session_path. Changing the order will fix the problem.
TL:DR; devise_for :users should be declared before resources :users.

Rails correct way to use link_to with devise

I'm getting started with rails and devise for authentication and I want to make a link to sign out when a user is logged into the admin page.
What is the correct way to write the link_to code
Here's my rake routes:
admin_index /admin/index(.:format) {:controller=>"admin/home", :action=>"index"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
home_index GET /home/index(.:format) {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
I tried <%= link_to "Sign Out", destroy_user_session_path %> but when i click the link it gives me the error:
No route matches [GET] "/users/sign_out"
From this devise sample application, recommended on the Devise wiki:
<% if user_signed_in? %>
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
<% end %>
the root error of your problem is that you haven't use RESTful routes in your "link_to".
you should correct your code to:
<%= link_to "Sign Out", destroy_user_session_path, :method => :delete %>
so that it will match the routes
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations" }

Resources