Rails routing error: receiving Unknown Action exception - ruby-on-rails

In my routes.rb file, I have the following:
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destory
end
And in a view file, I have this link:
<%= link_to 'Logout', logout_url, :method => :delete%>
But when click the Logout link, I get this error
Unknown action
The action 'destory' could not be found for SessionsController

Your route is fine, the problem is that your SessionsController has no destroy action defined
Actually, I just noticed, your route has destory instead of destroy

Related

No route matches [GET] "/logout"

In my session controller I have a method called destroy who is pointed in the route as "Logout", the function of it is only reset the session variable to nil and redirect to the store index:
def destroy
session[:user_id] = nil
redirect_to store_url, notice: "Logged out"
end
In the route file I declared the pointer:
get "sessions/destroy"
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Now in the browser I should trigger the HTTP DELETE VERB, but instead it takes the GET and the route can't be found it.
For example:
localhost:3000/logout, the error is No route matches [GET] "/logout"
In the link you should put explicit method: :destroy, like this:
= link_to 'Destroy', session, method: :destroy
If you look into your Devise initializer in config/initializers/devise.rb and seek through line 236 and switch to :get if you want your users to sign out via a GET request.
config.sign_out_via = :get
I had the same error..
this worked for me.
Just changed the routes from..
delete 'logout', to: 'sessions#destroy'
#to
get 'logout', to: 'sessions#destroy'

Rails 4 - uninitialized constant SessionController

I'm working on a Rails app in which I have a Session controller with the following method:
def destroy
session[:user_id] = nil
redirect_to root_path, notice: "Utloggad"
end
In my routes file I have the following routes:
controller :session do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
And my view looks like this:
= link_to "Log out", logout_path, method: :delete, :class => "small"
When I press the link I get the following error: uninitialized constant SessionController. How can I solve this?
Without your code I can't be sure but if you are using devise it should probably be SessionsController and not SessionController
so your code should look like
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end

ruby on rails button_to not activating the delete method

Hi im following the agile web development ebook and i cant seem to activate the logout action
here are the revelant parts (TAB key not working could not format to code)
rake routes
logout DELETE /logout(.:format) sessions#destroy
from the route file
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
my controller
def destroy
session[:user_id] = user.id
redirect_to store_url , notice: "Logged out"
end
and my view (relevant part)
<%= button_to 'Logout', logout_path, method: :delete %>
the error message is
No route matches [GET] "/logout"
i know it should use delete method but nothing i do seems to help
You may need to add a match in your routes. Sorry that I don't have the book with me to refer to.
Put this above your controller :sessions ...
match 'logout' => 'sessions#destroy', :as => :logout
If you didn't put the above line, your logout path should be sessions_logout_path, not logout_path.
Reference:
http://guides.rubyonrails.org/routing.html#naming-routes
match '/logout' => 'sessions#destroy', :via => :delete
or
controller :sessions do
member do
delete :destroy, :as => :logout
end
end

Ruby unknown action error

In AdminController I am having two methods 1. update 2. update_admin
in admin/admin_edit.erb
>% form_for(#admin, :url => update_admin_admin_path, :method => post, :html => {:id => 'user_edit_form'}) do |f| %>
In routes.rb
resources :admin
member do
post :updaate_admin
end
end
On form post I am expecting the url '/admin/update_admin/2' but the url '/admin/2/update_admin' is triggered. Because of this I am getting the error
The action '2' could not be found for AdminController
On form post action I want to call update_admin method. How to do this?
Instead of post :update_admin I modified put :update_admin. Now it works fine for me.

redirect_to and :controller, :actions getting mixed up in Rails

In a Rails controller I'm calling this:
redirect_to :controller => "user", :action => "login"
My user_controller.rb defines the following method:
class UserController < ApplicationController
def login
###
end
When I call my redirect_to method I get this error in my browser: No action responded to show. Checking the server logs I see this line:
Processing UserController#show (for 127.0.0.1 at 2009-12-19 12:11:53) [GET]
Parameters: {"action"=>"show", "id"=>"login", "controller"=>"user"}
Finally, I've got this line in my routes.rb
map.resources :user
Any idea what I'm doing wrong?
The hack-y answer is to change the following line in your routes.rb
map.resources :user
to
map.resources :user, :collection => { :login => :get }
But that's bad for a variety of reasons -- mainly because it breaks the RESTful paradigm. Instead, you should use a SessionController and use sessions/new to login. You can alias it to /login by the following line in routes.rb
map.login 'login', :controller=>"sessions", :action=>"new"
Tried
redirect_to :url => {:action => 'login', :controller => 'user'}
Or you might have to remove the routing or write your own. The routing maps POST to create, PUT to update etc.
You need to add the login action in your routes.rb. Refer to this excellent guide.
What I ended up doing was creating a new login_controller to handle logins.
Here's the relavent bit from my routes.rb file:
map.login '/login', :controller => 'login', :action => 'login'
Thanks for your help...

Resources