resources :users do
get :save_delete, :on => :member
get :question_form, :on => :member
post :question_send, :on => :member
resources :comments
end
After send and POST to my server I got this error in the log:
#Started POST "/users/1/question_send"
#ActionController::RoutingError (No route matches "/users/1/question_send")
rake routes # works fine, no errors
You most likely haven't restarted your server. The routing code looks fine.
However, I agree with #mathepic - your resource seems rather odd. There may be something we don't know, but unless you have a really really really good reason for these question_form and question_send member routes, you should probably think about doing this some other (and more Rails-y) way.
Related
I have been working my way through the Railscasts videos and trying to adapt them for the latest
version of rails, being version 6. I am up to the following episode:
http://railscasts.com/episodes/124-beta-invitations
The video explains that you need to have some form of authorisation/authentication in place prior to the changes,
so I followed the instructions on the following site to do so:
https://www.nopio.com/blog/authentication-authorization-rails/
I stopped at Step 20 as the final step was not needed. Here devise is the main gem used for most
of the heavy lifting.
Now as far as the railscast goes I am at the point where we need to alter the signup path, which is where I am stuck, and the example given, of a change to routes file, is as follows:
map.signup '/signup/:invitation_token', :controller => 'users', :action => 'new'
There is also a comment from some time after going down the same path as me and using devise
which shows the following:
devise_for :users, :controllers => {:registrations => 'registrations'} do
get 'users/sign_up/:invitation_token' => 'devise/registrations#new', :as => "new_user_registration"
end
After entering the above and running rails routes, I cannot find any changes to the entry, ie. the addition of the :invitation_token and the sign_up path still looks as follows:
new_user_registration GET /users/sign_up(.:format) registrations#new
My understanding is it should look like:
new_user_registration GET /users/sign_up/:invitation_token(.:format) registrations#new
If someone could point in the right direction, it would be greatly appreciated :)
Please let me know if any additional information is required to assist in a solution?
as far as I'm aware devise doesn't let you modify the default registration path using devise_for (see the method docs: https://www.rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper%3adevise_for)
what you can do instead is use the devise_scope.
devise_scope :user do
post 'users/sign_up/:invitation_token', to: 'registrations#new', as: 'new_user_registration'
end
note that because you are using your own registrations controller you need to point the new path to use this controller, not the devise one.
Say I have an object called invoice. In routes.rb I have
resources :invoices do
get "pay"
end
When I run rake routes, the route is generated as
invoice_pay GET /invoices/:invoice_id/pay(.:format) invoices#pay
and the parameter is :invoices_id instead of :id
If I use a match statement:
match "invoices/:id/pay" => "invoices#pay", :via => :get
I get:
GET /invoices/:id/pay(.:format) invoices#pay
It seems to me that the route should be pay_invoice_path(#invoice), however, I have not found suitable documentation on this. Any suggestions?
i think what you are trying to do is
resources :invoices do
get "pay", :on => :member
end
have a look at the guides: http://guides.rubyonrails.org/routing.html
I have routes that work perfectly on one machine, but on another machine they are failing and I've had a hard time to figure out what is wrong. On the failing machine it return the following errors for get /groups/my and groups/ respectively
No route matches {:controller=>"groups/owner/static_content", :topic=>"general"}
No route matches {:controller=>"groups/static_content", :topic=>"general"}
I have no idea where
static_controller
and
:topic=>"general"
come from since they don't appear anywhere in my routes file. Basically I have a route like
namespace :groups , :as => nil do
root :to => 'groups#index'
resources :groups, :only => [:show, :new, :create], :path => '' do
collection do
get :search
get 'my' => 'owner/groups#my', :as => :my
end
member do
post :subscribe
end
... other resources within a group
end
end
Any idea what I have done wrong or I'm missing? I'm using rails 3.2.2 and ruby 1.9.3 on rvm
A route is usually called from the views, so always check your view for action_controller_name_path if Controller::Action can not be found!
I'm working through Michael Hartl's excellent tutorial on Rails, but I am having trouble with exercise 7 in Chapter 11.
This exercise is:
Add a nested route so that
/users/1/microposts shows all the
microposts for user 1. (You will also
have to add a Microposts controller
index action and corresponding view.)
I've done this successfully by changing my routes.rb file to read:
resources :users do
resources :microposts, :only => [:create, :destroy]
end
I am able to successfully call /users/1/microposts from a browser. However, most of the tests in microposts_controller_spec.rb are now broken. I receive the "no route matches" error when running autotest. For instance, the first test, which simply reads:
it "should deny access to 'create'" do
post :create
response.should redirect_to(signin_path)
end
now produces the following error:
1) MicropostsController access
control should deny access to 'create'
Failure/Error: post :create
No route matches {:controller=>"microposts",
:action=>"create"}
When I check rake routes
, I find this entry:
user_microposts POST /users/:user_id/microposts(.:format) {:action=>"create", :controller=>"microposts"}
which suggests the route does exist.
Has anyone else run into this issue while completing the tutorial? Is there a change I need to make in the spec file once I introduce nested routes? Does Rspec work with nested routes?
thanks
Because this is a nested route you will need to pass the user_id through:
some_user = way_of_creating_a_user_goes_here
post :create, :user_id => some_user.id
RSpec will attempt to go to the /microposts route without this parameter.
In rails 2 you can use the :any option to define a custom route that responds to any request method e.g.
map.resources :items, :member => {:erase => :any}
rails 3 doesn't seem to support the :any option
resources :items do
get :erase, :on => :member # works
any :erase, :on => :member # doesn't work
end
does anyone know if this option has been removed or just renamed?
From digging around and seeing what the get, post, put, and delete actions actually do in ActionDispatch, I think all you need to do is match. So:
resources :items do
get :erase, :on => :member
match :erase, :on => :member
end
I don't think that syntax for match is actually documented, but the routes it constructs are, atleast for me, what you'd expect from an all method
Good question.
Looking at the Edge Rails routing guide and the Rails 3 source it doesn't look like it's supported. You could raise a ticket in the Rails Lighthouse (I couldn't find an existing one for this).
Match will work, but not inside a resources definition unfortunately. I rather wish they'd bring back a way to define get/post at least together..