I am trying to change the param in my nested resource but for some reason it add the name of the parent resource
routes.rb
resources :users, path: 're', param: :username do
resource :profile
end
this is how my routes are looking right now
user_profile POST /re/:user_username/profile(.:format) profiles#create
new_user_profile GET /re/:user_username/profile/new(.:format) profiles#new
edit_user_profile GET /re/:user_username/profile/edit(.:format) profiles#edit
GET /re/:user_username/profile(.:format) profiles#show
PATCH /re/:user_username/profile(.:format) profiles#update
PUT /re/:user_username/profile(.:format) profiles#update
DELETE /re/:user_username/profile(.:format) profiles#destroy
users GET /re(.:format) users#index
POST /re(.:format) users#create
new_user GET /re/new(.:format) users#new
edit_user GET /re/:username/edit(.:format) users#edit
user GET /re/:username(.:format) users#show
PATCH /re/:username(.:format) users#update
PUT /re/:username(.:format) users#update
DELETE /re/:username(.:format)
How can I change /re/:user_username/profile/new(.:format) for /re/:username/profile/new(.:format)
Related
I am working on a project which includes signup, login and users can post articles. However I want to include username of the user in article url. So I did like this in routes.rb
scope '/:username' do
resources :articles, :path => '/status', only: [:create, :destroy, :show]
end
Now when I open project index which shows all articles, I am getting this error.
No route matches {:action=>"create", :controller=>"articles"} missing required keys: [:username]
Am I doing something wrong?
Edit:
Here is the output of rake routes
Prefix Verb URI Pattern Controller#Action
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
root GET / static_pages#home
help GET /help(.:format) static_pages#help
signup GET /signup(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
GET / users#index
POST / users#create
GET /new(.:format) users#new
GET /:id/edit(.:format) users#edit
GET /:id(.:format) users#show
PATCH /:id(.:format) users#update
PUT /:id(.:format) users#update
DELETE /:id(.:format) users#destroy
archings POST /:username/status(.:format) articles#create
arching GET /:username/status/:id(.:format) articles#show
DELETE /:username/status/:id(.:format) articles#destroy
Here is the code of routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users, shallow: true do
resources :articles
end
end
Although having really short URLs may be attractive you should not use a wildcard at the "lowest level" of your url schema - unless your application only really deals with a single type of resource. Adding a wildcard at the bottom level in your urls causes huge headaches since it will swallow every other route unless great care is taken.
I would really recommend that you use /users/:username/articles.
resources :users, shallow: true do
resources :articles
end
This is a better restful design and it means that you do not have to blacklist usernames against a list of the other urls (and future urls) needed in your application.
Added:
The output of $ rake routes:
Prefix Verb URI Pattern Controller#Action
user_articles GET /users/:user_id/articles(.:format) articles#index
POST /users/:user_id/articles(.:format) articles#create
new_user_article GET /users/:user_id/articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#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
I'm a rails beginner struggling with routes a bit. I'm working on a Q&A site (or question and response) and any time I try to post a response, it gives the following error:
No route matches [GET] "/questions/6/responses"
Any help would be greatly appreciated,
Jon
Here are the relevant bits:
Routes output (given w the error)
(apologies for nasty paste):
Helper HTTP Verb Path Controller#Action
Path / Url
welcome_index_path GET /welcome/index(.:format) welcome#index
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
categories_path GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category_path GET /categories/new(.:format) categories#new
edit_category_path GET /categories/:id/edit(.:format) categories#edit
category_path GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
question_responses_path POST /questions/:question_id/responses(.:format) responses#create
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
root_path GET / welcome#index
Routes:
Rails.application.routes.draw do
get 'welcome/index'
resources :users
resources :categories
resources :questions do
resources :responses, :only => [:create]
end
root 'welcome#index'
Questions controller
https://github.com/joncarpe/snack/blob/master/app/controllers/questions_controller.rb
Responses controller
https://github.com/joncarpe/snack/blob/master/app/controllers/responses_controller.rb
You have only route to create action in ResponsesController. If you also want route to index, you should have:
resources :responses, only: [:create, :index]
If you want routes to all default resources actions, you should abandon only option, like this:
resources :responses
Lets say I have an UsersController that contains an action #new. In my routes file I map with the following:
match 'signup', to: 'users#new'
This action can now be accessed by both /signup and /users/new. How do I restrict it to only the custom route.
I apologize if this has been answered, but am new to this. I've searched, but haven't found the answer. Possibly due to my not knowing how to concisely phrase this.
You can exempt the new route from the users resource, and replace it with your custom route:
resources :users, except: [:new]
get 'signup', to: 'users#new', as: "new_user"
Resulting in:
users GET /users(.:format) users#index
POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
new_user GET /signup(.:format) users#new
I have the following in my Rails routes.rb:
resource :sign_up, only: [:new, :create]
resources :users
get 'users/activate/:token' => 'users#activate', as: 'activate_user'
Which gives me the following routes:
Prefix Verb URI Pattern Controller#Action
sign_up POST /sign_up(.:format) sign_ups#create
new_sign_up GET /sign_up/new(.:format) sign_ups#new
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
activate_user GET /users/activate/:token(.:format) users#activate
I'd like to get rid of the get 'users/activate/:token' ... route and use nesting or scoping instead, though I can't figure it out. Is there a way to accomplish this?
Thanks!
You can set up a collection route for users:
resources :users do
collection do
get 'activate/:token', :action => :activate, :as => :activate
end
end
And it will give you routes like this:
Prefix Verb URI Pattern Controller#Action
activate_users GET /users/activate/:token(.:format) users#activate
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
For my test I have the following:
test "should update holder" do
holder = Holder.create(name: "name", user_id: 10)
put :update, holder: holder
assert_redirected_to holder_path(assigns(:holder))
end
And when I run them I get the following Error:
ERROR (0:00:00.185) test_should_update_holder
No route matches {:holder=>"980190963", :controller=>"holders", :action=>"update"}
# /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:520:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:561:in `generate'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:147:in `assign_parameters'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:453:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:49:in `process'
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:390:in `put'
test/functional/holders_controller_test.rb:36:in `block in <class:HoldersControllerTest>'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:458:in `_run__4148286245602197272__setup__4285546581512185515__callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
But in my routes I have:
Teacherjoy::Application.routes.draw do
get "users/new"
resources :questions
resources :pages
resources :holders
resources :users
resources :sessions, only: [:new, :create, :destroy]
root :to => 'pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
end
and rake routes returns:
[teacherjoy (master)]$ rake routes
users_new GET /users/new(.:format) users#new
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
holders GET /holders(.:format) holders#index
POST /holders(.:format) holders#create
new_holder GET /holders/new(.:format) holders#new
edit_holder GET /holders/:id/edit(.:format) holders#edit
holder GET /holders/:id(.:format) holders#show
PUT /holders/:id(.:format) holders#update
DELETE /holders/:id(.:format) holders#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
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
If you look at rake routes, there clearly is a an action for update, which is a put, in the holders controller, which is what my test is doing, right?
Notice that the route is actually PUT /holders/:id, but you're passing a :holder option to your put method, not an :id. Try changing that line in your test to this:
put :update, id: holder