Yet when run rake:routes it appears to be there:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
root /(.:format) {:controller=>"pages", :action=>"home"}
Here are the routes from routes.rb:
resources :users
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/contact', to: 'pages#contact'
match '/about', to: 'pages#about'
match '/help', to: 'pages#help'
It's possible that you are not passing the :id param in your route, which is why the route is not matched, since :id is required:
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
Note: The parentheses around the :format param mean that it is optional.
I got the same error as well.But the reason is the little mistake that in the view page I write
<%= form_for(:session,url:session_path) do |f| %>
which I less the last 's' of 'sessions'.
This looks like an error I was running into when running through http://ruby.railstutorial.org/, and it turned out that I had left a few things out of routes.rb. The addition of the resource route is accompanied by the following two additional routes:
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
It's hard to see because the first route in that group is already there, so I had just glossed over the group (several times) as already being there.
resources controller adds map to method
{:action=>"method", :controller=>"controller"}
when in your case the rails seemed to ask for explicitly the map as
{:controller=>“controller”, :action=>“method”}
The :controller is before :action
This also answered Noach's question why match '/signout', :to => 'sessions#destroy' has to exist, if you rake:routes you will see it added
{:controller=>“sessions”, :action=>“destroy”} while there is already a
{:action=>“destroy”, :controller=>“sessions”} added by resources sessions
Related
I have been building devise into a small Rails app. It's a pretty standard setup but one wrinkle is that the default authentication rules need to be changed for this project. Non-idempotent registration actions must all be restricted to only authenticated users.
To do this, I've written a custom registrations controller and linked it up in the routing. All the custom controller really has to do is alter the filtering requirements. Based on the Rails controller filtering documentation, I got this working with a minimum of effort.
But by itself that only applies to devise-specific actions like /users/sign_up or /users/edit. I would also like to wire in the standard Rails actions (/users/new, /users/1/edit, etc) to the custom controller. I can get the action to instantiate the controller -- leading the horse to water so to speak -- but when the action is new instead of sign_up, it crashes out during the filtering.
The problem appears to be that resource_name yields nil when the action is 'new', but is fine when it's 'sign_up'. I don't understand why devise cares or where the distinction is represented in code. I've looked at other questions like Update the User controller of devise and Override devise registrations controller and Custom Devise controller but they don't get into this issue.
The specific error I am getting is
NoMethodError in RegistrationsController#new
undefined method `name' for nil:NilClass
and the top of the trace is
devise (2.1.2) app/controllers/devise_controller.rb:22:in
resource_name' app/controllers/registrations_controller.rb:104:in
authenticate_scope!'
It is apparently happening the first time devise_mapping is referenced. But as per the above stackoverflow question I have also explicitly provided helpers for devise_mapping, and they don't even get called.
Can anyone explain or help me figure out where to look further?
from routes.rb:
Fs::Application.routes.draw do
devise_for :users, :controllers => { :registrations => 'registrations' }
resources :users, :except => [:index, :show], :controller => 'registrations'
resources :users, :only => [:index, :show]
[...]
rake routes reports:
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
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users POST /users(.:format) registrations#create
new_user GET /users/new(.:format) registrations#new
edit_user GET /users/:id/edit(.:format) registrations#edit
user PUT /users/:id(.:format) registrations#update
DELETE /users/:id(.:format) registrations#destroy
GET /users(.:format) users#index
GET /users/:id(.:format) users#show
my registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
prepend_before_filter :authenticate_scope!, :except => [:index, :show]
skip_filter :require_no_authentication
end
my users_helper.rb:
module UsersHelper
# used by devise
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
using:
Rails 3.2.6
Ruby 1.9.3
devise 2.1.2
Update: pasted in the correct trace output.
So one solution appears to be to add
devise_scope :user do
get '/users/new' => 'registrations#new'
end
into routes.rb. This may not be the best answer but it works.
Suppose I have an events controller. If I request /events by GET it will respond with index action in news controller, this view will display future events.
I want to add a new action called past, it is like index, which return an array of Events but with another query.
I have added to routes.rb this chunk:
resources :events do
collection do
get :past
end
end
and I this chuck to events_controller:
def history
#events => Event.past
render :template => 'index'
end
But when I request http://127.0.0.1:3000/events/past in browser it does not work, in log are thrown these lines:
Started GET "/events/past" for 127.0.0.1 at 2012-04-02 19:32:01 -0500
Processing by EventsController#show as HTML
Parameters: {"id"=>"past"}
And finally here you have rake routes output:
events GET /events(.:format) {:action=>"index", :controller=>"events"}
event GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
past_events GET /events/past(.:format) {:action=>"past", :controller=>"events"}
contact_us_event POST /events/:id/contact_us(.:format) {:action=>"contact_us", :controller=>"events"}
GET /events(.:format) {:action=>"index", :controller=>"events"}
POST /events(.:format) {:action=>"create", :controller=>"events"}
new_event GET /events/new(.:format) {:action=>"new", :controller=>"events"}
edit_event GET /events/:id/edit(.:format) {:action=>"edit", :controller=>"events"}
GET /events/:id(.:format) {:action=>"show", :controller=>"events"}
PUT /events/:id(.:format) {:action=>"update", :controller=>"events"}
DELETE /events/:id(.:format) {:action=>"destroy", :controller=>"events"}
Other test was check routes using Rails console:
$ script/rails c
1.9.2-p318 :001 > rs = ActionController::Routing::Routes
1.9.2-p318 :002 > rs.recognize_path "/events/past", :method => :get
=> {:action=>"show", :controller=>"events", :id=>"past"}
What's wrong?
The problem was on routes precedence, here you have a routes.rb chunk that works:
resources :events, :only => [:index, :show] do
get 'past', :on => :collection
post 'contact_us', :on => :member
end
Try:
resources :events do
get :past, :to => "events#past"
end
I don't think you need the collection context. (And you might not need the :to part)
Update
Another way to do it is to put the event call above the resource. Assuming you don't need the collection with it.
get 'events/past', :to => "events#past"
resources :events
Should provide you with the correct routes.
For reference: Rails 3 routes: How to avoid conflict with 'show' action?
I have a form that a user submits. The form is for a user. I have the following in my routes.rb file:
resources :users, :except => [:new]
root :to => 'pages#home'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
In the users_controller.rb:
def create
#title = "Home"
#user = User.new(params[:user])
respond_to do |format|
if #user.save
format.html { redirect_to user_path(#user), :flash => {:success => "Your friend's details have been saved.The surprise has begin"} }
format.js
else
format.html { render :template => 'pages/home'}
format.js
end
end
end
This form is being submitted with a regular POST request and not Ajax.
I want to know why the Update template is being requested after the form is successfully submitted instead of the Show.
The output from rake routes is as follows:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
root /(.:format) {:controller=>"pages", :action=>"home"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
Thanks in advance.
What is the code for the controller and view of this form? Is there an existing user object when you generate the form or do you create a new user object (ie. #user = User.new in your controller)? If there is an existing user then the form will submit to update.
Your route and controller code look good, i.e. it should be sending you to the show method in your users_controller. I'd put some debbugging code in there, i.e. logger.debug("Inside Show") to see if the show method was being called.
You could also include the output of rake routes in your edited question, it might be informative to show where the user_path helper is going to send the redirect.
ian.
I am using resources :users in routes.rb. This provides the following paths as rake routes unveils.
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
Further, I comment out the legacy wild controller route.
#match ':controller(/:action(/:id(.:format)))'
To add a delete link on my users page I add the following.
<%= link_to "Delete user", user, :method => :delete, :confirm => "Are you sure?" %>
This generated the following html code.
Delete user
I click the link and it takes me to the show page?! What's wrong?
You need to include the default Javascript files for that to work properly:
<%= javascript_include_tag :defaults %>
I'm trying to add a link so the user can destroy his/her own account. I'm using the built-in registration class.
In my view I have <%= link_to 'Destroy', current_user, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %> pointing to localhost:3000/users/4 by example
First of all, is that the correct link to use?
Secondly, how to redirect to root path because presently it looks like it tries to redirect to user with id 4 (and it fails because it is protected).
Rake routes gives DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
Thanks in advance.
Try
<%= link_to 'Destroy', user_registration_path, :confirm => 'Are you sure you want to destroy your account?', :method => :delete %>
It's because of devise treat registration as Singular Resource.
Besides, run rake routes and you can see details about registration routing:
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"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
It means:
user_registration_path is a helper method that returns /users(.format)
Perform DELETE request on /users(.format) will delete the registration