Consider this in routes.rb
resource :places do
resource :people
get 'search'
end
When I do this in browser:
localhost:3000/places/search
It gives me invalid id for place error.
I'm looking to do this without using a "match"
You need to specify that the get applies to the collection:
resource :places do
resource :people
get 'search', :on => :collection
end
See the documentation on collection routes for details.
resources :places do
resources :people
get 'search', :on => :collection
end
Related
I have this routes and it's routeing to /USERS/:username once all is said and done and I need it to route to /:username without the leading USERS.
resources :users do
get :autocomplete_user_name, :on => :collection
end
autocomplete_user_name_users GET /users/autocomplete_user_name(.:format) users#autocomplete_user_name
You can do:
resources :users, path: '' do
get :autocomplete_user_name, :on => :collection
end
please can you explainme what does this code do?
resources :products do
get :who_bought, :on => :member
end
the code complete is from the book pragmatig programing, but it not explain why we use that code, ":on => :member""
Depot::Application.routes.draw do
resources :orders
resources :line_items
post 'line_items/decrease'
resources :carts
get "store/index"
resources :products do
get :who_bought, :on => :member
end
root :to => 'store#index', :as => 'store'
thanks
passing :on => :member means that you are working on a specific record in the database, in this case products. so the url that route generates is
/products/:id/who_bought
which means that you want the get the product whose id is :id and process the who_bought action. the counterpart, :on => :collection, expects the action to work on a list of products so the url will look like
/products/who_bought
if you change member to collection. you can see that the route doesn't require an :id passed because it doesn't expect you to work on a single record.
I have a page /sessions that displays a form.
I want to to post to itself and then display the results.
My routes currently look like this
namespace :api do
namespace :v1 do
resources :users do
match :all_users, :on => :collection, :as => "/users"
end
resources :sessions, :only => [ :index ]
resources :user_info do
match :user_info, :on => :collection
end
resources :user_schedule do
match :user_schedule, :on => :collection
end
end
so sessions now only shows the index, but I want it to respond to both get and post requests, and display my json results if it is a post request.
Eventually I'd like all other routes to follow the same
Thanks
Use
resource :sessions
Singular. That creates singular resources, as explained in here
You still go to the different actions in the controller depending on the http method, so you can choose what to render in each case.
In my routes.rb:
resources :posts do
get "test"
end
This produces the usual RESTful routes with /post/:id/.... However, I also get /post/:post_id/test.
Now my problem is that sometimes the parameter is named :id and sometimes it is :post_id. How can I make it uniform?
Thank you!
Specify :on => :member, otherwise it is acting as a nested resource.
resources :posts do
get 'test', :on => :member
end
You shouldn't make it uniform. It's :id when it's the target resource and :post_id when it is the parent of some other target resource (i.e. nested resources). This is a Rails convention.
i have probably a simple question. I have created a namespace panel with categories controller.
After creating or editing a category, rails redirects me to website.com/categories/:id instead of website.com/panel/categories/:id.
I've noticed that in the _form view, the #panel_categories argument of form_for() function points to /categories nor /panel/categories and that's causing this behaviour. Offcourse i can add a :url => '/panel/categories' param but i feel that it's not the best solution...
Can you provide me any better solution?
Thanks in advance
Files:
routes.rb:
Photowall::Application.routes.draw do
resources :photos
resources :categories
resources :fields
resources :users, :user_sessions
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
namespace :panel do
root :to => "photos#index"
resources :users, :photos, :categories, :fields
end
namespace :admin do
root :to => "users#index"
resources :users, :photos, :categories, :fields
end
end
categories_controller.rb:
http://pastebin.com/rWJykCCF
model is the default one
form:
http://pastebin.com/HGmkZZHM
form_for [:panel, #panel_category]
You can set the url to a route such as:
:url => panel_categories_path
I'm not sure what your route is, but this should work with your application.