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"
Related
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
I have this resource in routes.rb
resources :orders, only:[] do
collection do
post :user_order_confirmation
end
end
which creates a path like this in HTML views 'orders/carrier_order_confirmation'. Can we do something that this path looks like this 'orders/user_confirmation'?
You can do
resources :orders, only:[] do
collection do
post :user_confirmation, acion: :user_order_confirmation
end
end
This will give you the exact route as you are looking for. Here is the test :
$ rake routes | grep orders
user_confirmation_orders POST /orders/user_confirmation(.:format) orders#user_confirmation {:acion=>:user_order_confirmation}
You can overwrite the conventional routes by adding the following line in your routes.rb after you set the resource routes:
get "/orders/user_confirmation" => "orders#user_confirmation", as: "user_confirmation"
To clarify, here's what each part of that means:
[Routing Method] "/[Route You want]" => "[Controller]#[Controller Method]", as: "[Custom Route Name]"
Try this
post :user_order_confirmation, as: :user_confirmation
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 want to implement a search function in the controller, which contains "show, new, create, etc..."
I added in route.rb:
get 'apps/search' => 'apps#search'
and in apps_controller.rb:
def show
#app_info = App.find(params[:id])
end
def search
# get parameter and do search function
end
but each time when i request the /apps/search?xxx=xxx then it will be rendered by show... and then search?xxx=xxx is the parameter for method show...
should I rather create a new controller for search? Or is it possible to implement search as my requirements?
Your routes are incorrectly prioritized - somewhere else in your routes file (before the get 'apps/search' line) you have resources :apps, which defines a route that matches the same regex as apps/search.
Routes match from top to bottom, so if you check the output of rake routes, you'll see that your request to apps/search is actually matching apps/:id - which is the show route of your apps resource.
Either move the apps/search route above the resources :apps declaration, or alternatively declare your search route as part of the apps resource, eg.
resources :apps do
get :search, on: :collection
end
(this will define apps/search in the way you want).
For more information on routing: http://guides.rubyonrails.org/routing.html
I think you should edit route.rb as the following:
get 'apps/search' => 'apps#show'
The Rails' way to "say" search is a new route to the apps controller is using collection. So, for example, supposing you already have a resources :apps, you can do:
resources :apps do
get 'search', on: :collection #or you can use another HTTP verb instead of get
end
And that would give you:
search_apps GET /apps/search(.:format) apps#search
I have events which are accessible at /events/eventname I would also like the same events to be available at /events/PREFIX-eventname
I'm currently using the default:
resources :events in routes.rb
Initially idea was to add another routing statement
get "/events/prefix-:id" => "events#show"
Will adding an additional rule conflict with the resource statement? What's the best way of achieving this?
You're right! Just add another routing statement:
resources :events
get "/events/prefix-:id" => "events#show"
This will create two routes to #show action.