I'm having trouble nesting my routes. It would probably be easiest to show you the code. Here is my routes.rb file:
resources :leagues do
get 'delete', :on => :member
resources :league_relations do
get 'delete', :on => :member
end
end
Each League has_many :league_relations, and each LeagueRelation belongs_to :league. The delete route there is just a confirmation before the destroy action.
I am trying to use Rails' path helpers, but they are not working for some reason. Specifically, I am trying to do this:
new_league_league_relation_path
But this raises the error:
No route matches {:action=>"new", :controller=>"league_relations"}
Technically, the error is correct. There is no route matches 'league_relations#new', but shouldn't the URL created by this helper be
/leagues/:id/league_relations/new
Thanks for your help, I really appreciate it.
Actually route is exactly as you expect it to be. You just forgot to add :league_id
This will work:
new_league_league_relation_path(:league_id => 1)
Related
I am getting the following error:
Missing :action key on routes definition, please check your routes.
For this route
resources :groups do
post '/groups/:id/add', on: :member
end
I have looked at several other SO answers on this but am not able to find one that helps me. What action is it missing?
This is not the right way to define routes. Actually router's member/collection accepts a key-value pair to generate the route. As you are defining a member function it will be like:
resources :groups do
member do
post :add
end
end
It will generate a route like: groups/1/add
If you use collection then it will generate: /groups/add
resources :groups do
collection do
post :add
end
end
Hope you get the idea.
Or
You can define specific route for this particular action like below:
match "/groups/:id/add" => "groups#add", via: :post
Key action here means controller action. as in
resources :groups do
post '/groups/:id/add', on: :member, action: "add"
end
Rails can't infer the action from a path. But you can define the action instead and rails will automatically figure out the path:
resources :groups do
post :add, on: :member
end
I've been having trouble with named routes in rails 4 (Named route for non resource nesting).
I've moved onto something else, but still struggling with the same problem of named routes for non resource urls.
This is my route from rake routes:
GET /messages/:id/report/:reply_token(.:format) messages#report
messages POST /messages(.:format) messages#create
and my routes.rb
resources :messages, only: [:create] do
member do
get 'report/:reply_token', :action => 'report'#, :as => :message
end
end
Because of the problem I had in my post linked at the top, I'm trying to get a url to the /messages/:id/report/:reply_token route by doing the following:
"#{messages_url(#message, :host => "localhost:3000")}/report/#{#message.reply_token}"
But it's giving me this:
http://localhost:3000/messages.110/report/6bBw22TdaRYcQ3iVzW1ZwA
Why is there a . between the 'messages' and the '110' (message_id)?
Instead of #message, I've also tried #message.id in the messages_url(). I've also tried this: report_message_path(message_id: #message.id, reply_token: #message.reply_token) but got the same error as in my question linked above. I've also tried message_url() instead but it gives undefined method 'message_url'.
You are mixing up routes. messages_url is to generate a URL for create action which does not have ID in its route. Rails assumes 110 is the format and uses the second route (which is named as messages)
messages POST /messages(.:format)
As a solution, name your route like this and also add show action
resources :messages, only: [:create,:show] do
member do
get 'report/:reply_token', :action => 'report' , :as => :custom_message
end
end
And,
custom_message_url(#message, :host => "localhost:3000")
More about naming routes here.
Answerd here already - Rails _path helper generating path with format not id
I need a route to accept a request reports#show with an attached :query parameter and I can't figure out how to write it. It needs to respond to this link in my view:
= link_to report_path(query: params[:query]) do
config/routes.rb
resources :reports do
resources :chapters
resources :pages
end
Tried variations of: get '/reports/:id/:query', :as => 'reports_query' but I keep getting:
Routing Error
No route matches {:action=>"show", :controller=>"reports", :query=>"europe"}
Project is mostly RESTful but I'll take anything that works at this point. Thanks for any help.
You should define your route to query with code like this
# routes.rb
resources :reports do
get ':query', to: 'reports#show', on: :member, as: :query
end
It will generate path helper you can use that way
= link_to 'Query Report', query_report_path(#report, query)
I went through the same problem here, and I solved it using the default param while defining my routes.
get :twitter_form, defaults: { form: "twitter" }, as: :twitter_form, to: "campaigns#show"
So I have a route in routes.rb like this:
get "customers/:id/payments", :controller=>"customers", :action=>"payments"
What would be the UrlHelper that would generate this, if any, when doing this in a view:
link_to customer.name, customers_payments_path(customer)
(customers_payments_path is not valid)
get "customers/:id/payments", :controller=>"customers", :action=>"payments", :as => 'customer_payments'
http://guides.rubyonrails.org/routing.html#naming-routes
From the above link:
You can specify a name for any route using the :as option.
I like Gazler's answer if it's only a one-off route, but if you've already got resource routes for customers then I would define this route like this:
resources :customers do
member do
get :payments
end
end
This way, you would still have the standard customers_path and customer_path helpers you'd normally get from a resource route, but it would also generate customer_payments_path in a shorter syntax.
You need to add the :as parameter to add a name to the route, so you can access it from a url_helper:
get "confirmations/:code" => "confirmations#show", :as => "show_confirmation"
Then in your views/controllers/tests:
link_to "Confirm", show_confirmation_url(confirmation)
I would like to pass an extra parameter to a member route of a resource
something like:
resources :events do
member do
get 'register/:participant_type_id'
end
end
I could only accomplish it using a static match statement
Looking around the internet I saw that this might be possible in Rails 3.0.2. I'm using 3.0.1 and it certanlly is not.
Am I doing something wrong? or is it really not possible?
thanks
Try this:
resources :events do
member do
get 'register/:participant_type_id', :action => 'register'
end
end
Just to complete the answer with my little findings. It also confused me for quite a while.
In Rails3, the member route with parameters will not have the automatic generated xx_yy_path helper. You need to add it providing the :as => part, omitted the resources name.
Regarding the example provided, to get register_event_path and register_event_url, you need to define it like the following:
resources :events do
member do
get 'register/:participant_type_id', :action => 'register', :as => 'register'
end
end