I have a search scope for my users with the following route:
resources :users do
collection do
get :search
end
end
This however generates /users/search as url. I would like to have /search as url. I tried the following:
get '/search', as: :search
get '/search' => 'users#search', as: :search
get :search, to: 'users#search', as: :search
They don't seem to work since I keep getting routing errors. What would be the correct way to write it?
This one should work (without the leading '/') :
resources :users
get 'search' => 'users#search', as: :search
The named helpers for this route will be search_path and search_url
you can also use match:
match "/search", to: "users#search", via: "get"
Related
How can I also show a blog post without /posts/ in the URL?
http://www.anthonygalli.com/posts/i-walk-the-line
http://www.anthonygalli.com/posts/50-shades-of-galli
http://www.anthonygalli.com/posts/tips-for-regaining-momentum
Shortened Version (Goal of Question):
http://www.anthonygalli.com/i-walk-the-line
http://www.anthonygalli.com/50-shades-of-galli
http://www.anthonygalli.com/tips-for-regaining-momentum
I have post as a MVC.
routes.rb
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
Change your routes.rb to:
resources :posts, path: '/' do
collection do
get 'about'
get 'feed'
get 'subscribe'
end
end
For documentation on Rails routing check: http://guides.rubyonrails.org/routing.html
Your routes should be updated to look something like this:
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
get ':id' => 'posts#show', :as => :post_by_slug
That as option will be important for linking to your new route if you want to link to the /post/-less paths:
<%= link_to post.title, post_by_slug_path(post) %>
P. S. I believe that the new route will need to be listed last in your routes so that the routes before it can have precedence. (Otherwise, trying to visit /about would try to load a post with a slug named about instead of the posts#about action. You should try this and see if that's true.)
Resource generates the standard CRUD urls, so you have to specify the posts route specifically. Something like this
get '/desired-url' => 'posts#index'
So here I have my routes
concern :search do
scope '/search' do
get '/', to: 'users#search'
get '/schedule/:id', to: 'schedules#user'
end
end
concerns :search
scope '/dashboard' do
concerns :search
end
The problem is rails didn't give me path helpers. How can I have different path for my concerns based on where I call it. I'd like to have something like this
search_path
search_schedule_path
dashboard_search_path
dashboard_search_schedule_path
Finally I've figured it
concern :search do
scope '/search', as: :search do
get '/', to: 'users#search'
get '/schedule/:id', to: 'schedules#user', as: :schedule
end
end
concerns :search
scope '/dashboard', as: :dashboard do
concerns :search
end
and here is my path helper
search_path
search_schedule_path
dashboard_search_path
dashboard_search_schedule_path
I'm trying to setup a route that looks like this: acme.com/posts/:category/:status. Both :category and :status are optional. I wrote many variations, but none worked:
resources :posts do
match '(/:category)(/:status)', to: 'posts#index', as: 'filter', on: :collection
end
# Category Links
link_to "Questions", filter_posts_path(:questions)
link_to "Suggestions", filter_posts_path(:suggestions)
# Status Links
link_to "Published", filter_posts_path(params[:category], :published)
link_to "Draft", filter_posts_path(params[:category], :draft)
The idea is to be able to 1) filter by category, 2) filter by status, and 3) filter by both category and status if both are available. The current setup has also broken my /posts/new path, always redirecting to posts#index.
I had this variation and it seems working fine:
namespace :admin do
resources :posts, :except => [:show] do
collection do
get "/(:category(/:status))", to: "posts#index", as: "list", :constraints => lambda{|req|
req.env["PATH_INFO"].to_s !~ /new|\d/i
}
end
end
end
= CONTROLLER=admin/posts rake route
list_admin_posts GET /admin/posts(/:category(/:status))(.:format) admin/posts#index
You can use the more RESTful resources :posts (in config/routes.rb) and send the params in the query string.
With that approach, all parameters are optional and you're not limited to using predefined parameters.
Do this works for you?
resources :posts do
collection do
match '/:category(/:status)', to: 'posts#index', as: 'filter'
match '/:status', to: 'posts#index', as: 'filter'
end
end
Hope at least it helps!
You could try something like this:
match '/filter/*category_or_status' => 'posts#index', as: 'filter'
With this you can build your own filter path. Then you could parse params[:category_or_status] in your controller and get the category or status if they are given.
I need a route to accept a request reports#show with an attached :query parameter and I can't figure out how to write it. It needs to respond to this link in my view:
= link_to report_path(query: params[:query]) do
config/routes.rb
resources :reports do
resources :chapters
resources :pages
end
Tried variations of: get '/reports/:id/:query', :as => 'reports_query' but I keep getting:
Routing Error
No route matches {:action=>"show", :controller=>"reports", :query=>"europe"}
Project is mostly RESTful but I'll take anything that works at this point. Thanks for any help.
You should define your route to query with code like this
# routes.rb
resources :reports do
get ':query', to: 'reports#show', on: :member, as: :query
end
It will generate path helper you can use that way
= link_to 'Query Report', query_report_path(#report, query)
I went through the same problem here, and I solved it using the default param while defining my routes.
get :twitter_form, defaults: { form: "twitter" }, as: :twitter_form, to: "campaigns#show"
I have this custom route in my routes.rb
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses
And I have constructed a link like this:
<%= link_to business.name, business_permalink_path %>
However, whenever I visit the page with that link, I get this error:
No route matches {:controller=>"businesses", :action=>"show"}
I tried inverting the route order:
resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
This does not work. It works if I change the link to this:
The show action exists and is defined in the file controllers/businesses_controller.rb.I want to create a custom URL using my permalink.
I am new in Rails and I know I am just missing something. What am I missing?
Try this:
<%= link_to business.name, business_permalink_path(business.permalink) %>
Try this:
match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink
More here: http://railscasts.com/episodes/203-routing-in-rails-3