I am trying to create a nested route like this:
resources :storyboards, path: "" do
resources :stories, path:""
end
This renders the following path params:
Because the nested route is defined before the parent, Rails is ignoring our /new route for our parent controller. I was wondering if anyone has achieved what we're trying to (using path: "" for nested routes)?
I believe the two fixes would be:
Create separate new route for storyboards controller (to override the other)
Somehow get rails to render the nested routes under the parent
If anyone has experience in this, your insight would be greatly appreciated!
With help from #babar, we managed to fix the error with a small hack:
#config/routes.rb
get "new", to: "storyboards#new"
resources :storyboards, path: "" do
resources :stories
end
Related
I'm building a "Brand personality" tool that gives you a report based on the text you share on social media.
I have a model PersonalityReport and in routes I have resources :personality_reports.
A new feature is to offer a "diff" between two reports and I'm trying to work out the most "guessable" way to model this in routes.
Ideally I'd like GET /personality_reports/:personality_report_id/diff/:id or something along those lines, and while I could simply put that into routes as a GET route, is there a more Railsy way of specifying a route using the resources / collections style so that my routes.rb file is more easy to understand?
The 'neatest' way can think of is:
resources :personality_reports, param: 'personality_report' do
member do
get 'diff/:id', to: 'personality_reports#action', as: 'diff_route'
end
end
Where obviously to: is your controller#action, and as: is the name of your route. After running rake routes you will see this generates:
diff_route_personality_report GET /personality_reports/:personality_report_id/diff/:id(.:format) personality_reports#action
I think whatever you mentioned is good enough,
resources : personality_reports do
resources :diffs, only: [:show]
end
So, routes like below,
personality_report_diff GET /personality_reports/:personality_report_id/diffs/:id(.:format) diffs#show
NOTE: You can also make diff route in singular resource :diff if you want to make it as singular resource.
My situation I have a "Parent" model and controller. I want to know the best practice for adding independent pages such as a dashboard for users. My thought is that I can create a view dashboard.html.erb and inside the parent controller create a method of:
Parent controller
def dashboard
end
Routes.rb
get 'parents/dashboard'
I've done this once and it worked fine, but is was for a 'child' model.
When I run this same situation in the parent model I get the error
ActiveRecord::RecordNotFound in ParentsController#show
Couldn't find Parent with 'id'=dashboard
1.) All I've done is add a view, added the dashboard model to the controller, and placed get 'parents/dashboard' into the routes.rb and it tries to reference the show method??? Why?
2.) And is this the wrong way to add pages/actions to a rails application?
Do this:
#config/routes.rb
resources :parents do
get :dashboard, on: :collection #-> url.com/parents/dashboard
end
And is this the wrong way to add pages/actions to a rails application?
It's not "wrong", it's just ineffective, as demonstrated by your problem.
The problem you have is you've included your custom route below the resources :parents route. Because resources creates a /:id url which captures any requests sent to parents/:id, your "dashboard" request is being sent to the show action of the parents controller:
There are two remedies to your issue:
Put get 'parents/dashboard' above the resources :parents directive
Include an additional route to resources :parents (above)
You must remember that Rails matches your request with a route. That means the first route to match your request is processed.
So if you have...
#config/routes.rb
resources :parents
get "parents/dashboard"
... Rails will assume the dashboard is the :id in url.com/parents/:id, thus sending the request to show.
Apart from the very top code (the recommended answer), you could have the following:
#config/routes.rb
get "parents/dashboard", to: :dashboard
resources :parents
If you want to add an additional route with an :id then the syntax is different.
get 'parent_dashboard/:id', to: 'parents#dashboard'
Notice that the person string after get. This is used as the address of the website, when the user hits this, it would go to localhost:3000/parent_dashboard/1 if it is the first dashboard. You can exclude :id if you'd like. Of course, this would be different from the use case.
The second part of the route syntax is the :to, this method tells your app which controller and method to look at.
Hope this helps!
If you want to add a new view to the parents folder. Just do this:
parents_controller.rb
def dashboard
#parent = Parent.find(params[:id])
end
routes.rb
get '/parents/:id/dashboard', to: 'parents#dashboard', as: :parents_dashboards
resources :parents
Then in your parents/dashboard.html.erb view you can do everything that you can do in the parents/show view.
The link to your dashboard view would be parents_dashboards_path and you might have to use parents_dashboards_path(#parent) or parents_dashboards_path(parent) in certain circumstances.
This is an example of a custom path that works without using nested resources to accomplish access to the parent's dashboard.
I am using this approach in a project so I would like to hear any critique or comments on this approach. PEACE I'M OUTTA HERE!
Can anyone point me to info on how the name of the params object is generated?
I just spent far too long trying to access params[:venue_search] following a form submission, only to find I needed to be referring instead to params[:searches_venue_search] presumably because I've organised the venue_search resource into a "search" folder and/or because of the preceding "search" directory in the URL that I've specified in the routes.
What's the logic behind this, anyone?
The routes:
resources :venue_searches, controller: 'searches/venue_searches', model: 'searches/venue_search', only: [:create, :new], path: "/search/venues"
match "search/venues/show", to: "searches/venue_searches#show", as: :venue_search
Cheers!
get "search/venues/show", to: "search/venue_searches#show", as: :venue_search
This will generate following route
venue_search GET /search/venues/show(.:format) search/venue_searches#show
I have the following route:
#routes.rb
get "(/questions_groups/:group_id)/questions/new" => "questions#new", as: "new_question"
resources :questions
Id like calling the new_question_path(#question_group) where #question_group.id = 1 to return the path:
/questions_group/1/questions/new
Yet it returns:
/questions/new?group_id=1
When I remove the resources :questions I get the correct path but lose all my routes, how can I solve this problem?
Just call it something different. When you define the resource it comes loaded with a whole bunch of url helpers, in your case one of them is new_question, which is the same name as your custom route. If you're trying to replace the route for the new question then tell the resource not to define its own with:
resource :questions, except: :new
Try this
match "/questions_groups/:group_id/questions/new" => "questions#new", as: "new_question"
I have a resource - activities, that is nested under provider. everything is working great for all my restful resources.
I'd like to add a new action to list all activities regardless of provider. So I think that should not be nested.
I tried to do this like so:
resources :activities, only: [:list]
But this doesn't create a route when i rake routes, and I get the error:
No route matches [GET] "/activities/list"
How do I do this? Is this the right way to go about what I want to do - show a list of all providers activities with a different view / layout than the nested provider#activities action.
OK. I (re) read the manual and did what it said, and that worked. Go figure.
resources :activities do
get 'list', :on => :collection
end
So that adds the list action to the routes with path & url methods & the nested resources still work.