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
Related
I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here
I have Products page that use the ugly param to handle sorting.
products_path(sort: "asc")
# resulting in
/products?sort=asc
I'm trying to make the URL looks like /products/asc. So I'm playing with the routes:
# routes.rb
get "/products/:sort", to: "products#index", as: "products_path"
Now, going to /products/asc works perfectly fine.
But products_path(sort: "asc") still generate /products?sort=asc.
Is there a way to make it generate the pretty URL?
Thanks
[EDIT and ANSWER]
I typo the as:. Should be:
# routes.rb
get "/products/:sort", to: "products#index", as: "products"
Move get "/products/:sort", to: "products#index", as: "products" above resources :projects in routes.rb
I typo the as:. Should be:
# routes.rb
get "/products/:sort", to: "products#index", as: "products"
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 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"
At the moment, I have this line in routes.rb
map.resources :usernotes
so my paths are /usernotes/new etc
But I realise now I want them to be like /notes/new
Is there a way to change the line in routes.rb to do this?
Yep, it's super-easy:
map.resources :usernotes, :as => 'notes'
See section 3.7.4 here:
http://guides.rubyonrails.org/v2.3.8/routing.html#controller-namespaces-and-routing (for Rails 2.3.x)
or here for Rails 3: http://guides.rubyonrails.org/routing.html#naming-routes
This will give you /notes and /notes/new etc
resources :notes, :controller=>"UserNotes"
You'll use new_note_path etc