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
Related
I have a problem with the form_for Helper in Rails. I want to edit a Post in my Forum app
I am getting this error when trying to call the edit page:
ActionView::Template::Error (No route matches
{:action=>"show",
:category=>#<Post id: 1,
user_id: 1,
title: "Die Webseite ist nun online",
body: "<div>Viel Spaß euch allen. Und haltet euch an die ...",
category: "general/announcements",
slug: "die-webseite-ist-nun-online",
created_at: "2018-09-16 01:00:55",
updated_at: "2018-09-16 01:00:55">,
:controller=>"posts",
:id=>"die-webseite-ist-nun-online",
:locale=>:de},
possible unmatched constraints: [:category]):
This is my Edit action:
def edit
#post = Post.find(params[:id])
end
And this is the call to form_for
<%= form_for(#post) do |f| %>
What I have tried:
<%= form_for(#post, category: params[:category]) do |f| %>
params[:category] has the right value, I tested it to not be empty or nil
Providing the category param like that did NOT change the error I get!
Edit:
as requested my routes.rb:
Rails.application.routes.draw do
concern :paginatable do
get '(page/:page)', action: :index, on: :collection, as: ''
end
CATEGORY_FILTERS = /(?x)general\/suggestions|general\/member-introductions|
general\/announcements|off-topic\/jobs-and-projects|
off-topic\/miscellaneous|off-topic\/funny-stuff|
ruby-on-rails\/news|ruby-on-rails\/developers|
ruby-on-rails\/tutorials/
scope '(:locale)', locale: /en|de/ do
root 'forum#home'
get '/help', to: 'forum#help'
get '/about', to: 'forum#about'
get '/contact', to: 'forum#contact'
get '/general', to: 'forum#general'
get '/ruby-on-rails', to: 'forum#rubyonrails'
get '/off-topic', to: 'forum#offtopic'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users, concerns: :paginatable
resources :password_resets, only: %i[new edit create update]
scope '*category', category: CATEGORY_FILTERS do
get '', to: 'posts#index'
resources :posts, concerns: :paginatable
end
end
resources :account_activations, only: [:edit]
end
Rails Routes gives
Prefix Verb URI Pattern Controller#Action
root GET /(:locale)(.:format) forum#home {:locale=>/en|de/}
help GET (/:locale)/help(.:format) forum#help {:locale=>/en|de/}
about GET (/:locale)/about(.:format) forum#about {:locale=>/en|de/}
contact GET (/:locale)/contact(.:format) forum#contact {:locale=>/en|de/}
general GET (/:locale)/general(.:format) forum#general {:locale=>/en|de/}
ruby_on_rails GET (/:locale)/ruby-on-rails(.:format) forum#rubyonrails {:locale=>/en|de/}
off_topic GET (/:locale)/off-topic(.:format) forum#offtopic {:locale=>/en|de/}
signup GET (/:locale)/signup(.:format) users#new {:locale=>/en|de/}
login GET (/:locale)/login(.:format) sessions#new {:locale=>/en|de/}
POST (/:locale)/login(.:format) sessions#create {:locale=>/en|de/}
logout DELETE (/:locale)/logout(.:format) sessions#destroy {:locale=>/en|de/}
users GET (/:locale)/users(/page/:page)(.:format) users#index {:locale=>/en|de/}
GET (/:locale)/users(.:format) users#index {:locale=>/en|de/}
POST (/:locale)/users(.:format) users#create {:locale=>/en|de/}
new_user GET (/:locale)/users/new(.:format) users#new {:locale=>/en|de/}
edit_user GET (/:locale)/users/:id/edit(.:format) users#edit {:locale=>/en|de/}
user GET (/:locale)/users/:id(.:format) users#show {:locale=>/en|de/}
PATCH (/:locale)/users/:id(.:format) users#update {:locale=>/en|de/}
PUT (/:locale)/users/:id(.:format) users#update {:locale=>/en|de/}
DELETE (/:locale)/users/:id(.:format) users#destroy {:locale=>/en|de/}
password_resets POST (/:locale)/password_resets(.:format) password_resets#create {:locale=>/en|de/}
new_password_reset GET (/:locale)/password_resets/new(.:format) password_resets#new {:locale=>/en|de/}
edit_password_reset GET (/:locale)/password_resets/:id/edit(.:format) password_resets#edit {:locale=>/en|de/}
password_reset PATCH (/:locale)/password_resets/:id(.:format) password_resets#update {:locale=>/en|de/}
PUT (/:locale)/password_resets/:id(.:format) password_resets#update {:locale=>/en|de/}
GET (/:locale)/*category(.:format) posts#index {:category=>/(?x)gener...rials/, :locale=>/en|de/}
posts GET (/:locale)/*category/posts(/page/:page)(.:format) posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
GET (/:locale)/*category/posts(.:format) posts#index {:category=>/(?x)genera...torials/, :locale=>/en|de/}
POST (/:locale)/*category/posts(.:format) posts#create {:category=>/(?x)genera..torials/, :locale=>/en|de/}
new_post GET (/:locale)/*category/posts/new(.:format) posts#new {:category=>/(?x)genera...torials/, :locale=>/en|de/}
edit_post GET (/:locale)/*category/posts/:id/edit(.:format) posts#edit {:category=>/(?x)genera...orials/, :locale=>/en|de/}
post GET (/:locale)/*category/posts/:id(.:format) posts#show {:category=>/(?x)genera...rials/, :locale=>/en|de/}
PATCH (/:locale)/*category/posts/:id(.:format) posts#update {:category=>/(?x)gene...rials/, :locale=>/en|de/}
PUT (/:locale)/*category/posts/:id(.:format) posts#update {:category=>/(?x)genera...orials/, :locale=>/en|de/}
DELETE (/:locale)/*category/posts/:id(.:format) posts#destroy {:category=>/(?x)gene...rials/, :locale=>/en|de/}
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
Maybe try to specify all the options for the form_for helper:
<%= form_for :post, url: post_path(#post, category: params[:category]), method: :patch do |f| %>
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)
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 have a search page and a search button on it, and when I click on the search button, it gave me an error below:
No route matches [POST] "/searching"
And I want that when I click on the search button, It goes into the search page, and show result of search. below is search page controller:
def search
#students=Students.all
#blah = params[:tf_Zip]
puts #blah
if ( !params[:tf_Zip].blank? or params[:tf_Zip] !="" )
#user_zip = User.where(user_Zip: params[:tf_zip])
end
render 'search'
end
You can see above that I am render here to the search page, and below is routes.rb page:
resources :search, only: [:search, :create]
match '/searching', to: 'search#search', via: 'get'
And below is the rake routes:
Helper HTTP Verb Path Controller#Action
Path / Url
search_new_path GET /search/new(.:format) search#new
search_create_path GET /search/create(.:format) search#create
settings_new_path GET /settings/new(.:format) settings#new
educations_create_path GET /educations/create(.:format) educations#create
educations_destroy_path GET /educations/destroy(.:format) educations#destroy
professions_create_path GET /professions/create(.:format) professions#create
professions_destroy_path GET /professions/destroy(.:format) professions#destroy
communications_create_path GET /communications/create(.:format) communications#create
communications_destroy_path GET /communications/destroy(.:format) communications#destroy
availabilities_create_path GET /availabilities/create(.:format) availabilities#create
availabilities_destroy_path GET /availabilities/destroy(.:format) availabilities#destroy
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
sessions_path POST /sessions(.:format) sessions#create
new_session_path GET /sessions/new(.:format) sessions#new
session_path DELETE /sessions/:id(.:format) sessions#destroy
availabilities_path POST /availabilities(.:format) availabilities#create
availability_path DELETE /availabilities/:id(.:format) availabilities#destroy
communications_path POST /communications(.:format) communications#create
communication_path DELETE /communications/:id(.:format) communications#destroy
professions_path POST /professions(.:format) professions#create
profession_path DELETE /professions/:id(.:format) professions#destroy
educations_path POST /educations(.:format) educations#create
education_path DELETE /educations/:id(.:format) educations#destroy
settings_path POST /settings(.:format) settings#create
new_setting_path GET /settings/new(.:format) settings#new
search_index_path POST /search(.:format) search#create
root_path GET / static_pages#home
signup_path GET /signup(.:format) users#new
signin_path GET /signin(.:format) sessions#new
signout_path DELETE /signout(.:format) sessions#destroy
default_path GET /default(.:format) static_pages#default
GET /availabilities(.:format) availabilities#new
GET /communications(.:format) communications#new
GET /professions(.:format) professions#new
GET /educations(.:format) educations#new
GET /settings(.:format) settings#new
searching_path GET /searching(.:format) search#search
Kindly help me, waiting for your reply. Thanks
try this one, as you're sending your form with POST
match '/searching', to: 'search#search', via: 'post'
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