Params for namespaced resources (Rails) - ruby-on-rails

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

Related

rename rails route

I have a route/path using CcpaAcknowledgmentsController
/ccpa_acknowledgments
I would like the route to be, BUT I would still like it to use the CcpaAcknowledgmentsController
/customers/ccpa_acknowledgments
Since I have these two routes...
resources :customers
resources :ccpa_acknowledgments
match '/customers/ccpa_acknowledgments', to: 'ccpa_acknowledgments#index', via: [:get]
I keep getting a conflict stating
NoMethodError in CustomersController.
Is there a way to get the desired route I want without putting the method/code in the CustomersController?
This is the way to do that
resources :customers do
get :ccpa_acknowledgments, to: 'ccpa_acknowledgments#index', on: :collection
end
Inside the customers block for two reasons:
we are fine with the beginning of the path /customers
we don't want to mess with the other customers' routes. In this way your route inside the block is before the customers default routes and it's not seen as you are calling customers/:id with ccpa_acknowledgments as id because rails takes care of that for you defining that route before the show
Then
get :ccpa_acknowledgments
because we need the second part of the path /ccpa_acknowledgments
to: 'ccpa_acknowledgments#index'
we want to specify the controller and action pair, because we want to use the CcpaAcknowledgmentsController even though we're inside the customers block
on: :collection
because we don't want any :id inside our route. It's a route defined on the customers collection
alternative using resources as asked in the comment. Try
scope :customers do
resources :ccpa_acknowledgments, only: :index
end
but you need to put this before the resources :customers

What's the "Rails way" to route a "diff" between two instances of the same model?

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.

Rails Nested Routes path=""

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

Removing controller name from Rails URL route

This is my first Rails project, I am trying to piece things together slowly.
When I'm trying to view the page I generated using rails g controller <controller> <page>, I find myself going to 0.0.0.0:3000/controller/page.html, How can I configure it so that my route file globally allows viewing the page via the page name, rather than controller/page, if no such way exists, then how can I route controller/page.html to /page.html
I've looked around, and haven't really found any explanation, maybe I'm looking in the wrong places?
In config/routes.rb:
get '/page' => 'controller#action'
If your controller is:
class UsersController < ApplicationController
def something
end
end
Then config/routes.rb would be:
get '/page' => 'users#something'
For static pages you could want to use public folder though, everything you put there is directly accessible, for example public/qqqqqq.html would be accessed in localhost:3000/qqqqqq.html
We've just achieved this by using the path argument in resources method:
#config/routes.rb
resources :controller, path: ""
For you specifically, you'll want to make something like this:
#config/routes.rb
resources :static_pages, path: "", only: [:index]
get :page
get :other_page
end
#app/controllers/your_controller.rb
def page
end
def other_page
end
This will give you routes without the controller name. You'll have to define this at the end of your routes (so other paths come first)
Obviously this will form part of a wider routes file, so if it doesn't work straight up, we can refactor!
It sounds like this is a static page, so you can do as juanpastas says, or another option is to create a folder under your app/views directory to hold these pages. Maybe something like
app/views/static_pages/the_page.html.erb
Then in your config/routes.rb you can add:
match '/your_page_name', to: 'static_pages#the_page', via: :get

Rails routing not giving wanted result

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"

Resources