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"
Related
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
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
Can I group 3 routes into 1?
I have this in routes.rb:
# pagination links for resources
get '/countries/page/:page', :to => 'countries#index'
get '/cities/page/:page', :to => 'cities#index'
get '/spots/page/:page', :to => 'spots#index'
If you are looking for a one liner to DRY your thing up, you can always write ruby code directly in your routes.rb file like so:
%w(countries cities spots).each{|v| get "/#{v}/page/:page", to: "#{v}#index" }
I am trying to reach the following url in my rails app:
example.com/user12 # It should show user#show
exmple.com/wordpress # It should show category#show
My solution: (it does not work)
In the routes.rb I have added :path => '' to both categories and users in order to remove the controllers' name from the url.
resources :categories, :path => ''
resources :users, :path => ''
When I run rake routes, I have the following urls"
category GET /:id(.:format) category#show
account GET /:id(.:format) accounts#show
So I assumed that It should be working correctly, but surely not. It only works for category names and for usernames it shows ActiveRecord::RecordNotFound.
I know, my solution is wrong because I am confusing rails route system and because the resources :categories has more priority over resources :users, the category show page works fine.
So Is there any solution to solve an issue like this?
I have finally found the solution with constraints option. This option accepts regular expressions.
resources :categories, :path => '', constraints: { id: /wordpress|php/ }
Every category should be added manually in this way OR (I am not sure) maybe there is a way to list all categories from database automatically.
One way of doing that is to override the default rails routes, to do that remove the resources
I tested and this works
SampleRails4::Application.routes.draw do
get '/user12', to: 'users#show' #=> users/show
get '/wordpress', to: 'category#show' #=> assuming u have a controller and action
end
however then you have to update the rest of your code, Ex: users/show might be expecting the user id as a param read more about rails routing
I have the following problem:
I created one entity "Film" with the command "scaffold" and automatically added in my routes file "resources: films", and then I try to added an autocomplete via ajax, but always the calling ajax calls the "show" action instead of call the route that I added "autocomplete_term"
My routes files (routes.rb)
resources :films
I tried the following possibilities (routes.rb)
match 'films/autocomplete_term' => "films#index", :via=>:get
match "films/autocomplete_term/:term" => "films#autocomplete_term", :controller=>"films", :action=>"autocomplete_term", :as => :films_autocomplete, :via => :get
resources :films do
collection do
get 'autocomplete_term'
end
end
The route
** localhost.com:3000/films/autocomplete_term?term=a**
The ERROR
Couldn't find Film with id=autocomplete_term
app/controllers/films_controller.rb:28:in `show'
When I run the command rake routes
GET /films/autocomplete_term/:term(.:format) films#autocomplete_term
films_autocomplete
GET /films/autocomplete_term/:term(.:format) films#autocomplete_term
autocomplete_term_films
GET /films/autocomplete_term(.:format) films#autocomplete_term
Sorry for my English
And thanks in advance
The url to access this route
GET /films/autocomplete_term/:term
should be
localhost.com:3000/films/autocomplete_term/a
if you do localhost.com:3000/films/autocomplete_term?term=a it will think it is the show action, it will ignore the ?term=a
GET /films/:id
Thanks to Anezio, solved the problem, basicament had two things wrong:
1 - The route
match "films / autocomplete_term /: term"
2: The order in my routes.rb file (Rails routes are matched in the order they are specified)
resources: films
match 'films / autocomplete_term /: term' => "films # autocomplete_term"
The Final Solution: (routes.rb)
match 'films / autocomplete_term' => "films # autocomplete_term"
resources: films