For example, because my controller is called listings, whenever I post a new listing it is for example domain.com/listings/listing-name however I would like it to be domain.com/businesses/listing-name instead.
How can I do this?
Current routes for this controller:
resources :listings do
member do
post :leadcreate
post :storycreate
end
end
Use path option
resources :listings, :path => "businesses" do
end
If you also want to rename the route helpers, then
resources :listings, :path => "businesses", :as => "businesses" do
end
Related
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 this scope:
scope ":user_id", :as => "user" do
resources :boards, :controller => 'users/boards'
end
I get this route:
http://localhost/hyperrjas/boards/
I want a url without boards then on routes.rb I add:
scope ":user_id", :as => "user" do
resources :boards, :controller => 'users/boards', :path => '/'
end
That works great, but it is still accessible via "/boards" ... How do I prevent that? (I'm using Rails 3.1)
You shouldn't have to specify the controller names when using resources and in this caseI would use nested resources:
resource :user, only: :show do
resources :boards
end
This should give you the following:
/:user_id
/:user_id/boards
/:user_id/boards/new
/:user_id/boards/:id/
/:user_id/boards/:id/edit
and of course your restful routes!
I am running Ruby on Rails 3 and I would like to set up my routes in order to rewrite URLs using namespaces, except for an action (the index action).
In the routes.rb file I have:
namespace "users", :path => "user" do
resources :accounts
end
So, for example, URLs to "show"/"create new" accounts are:
http://<site_name>/user/accounts/1
http://<site_name>/user/accounts/new
I would like to rewrite/redirect those URLs, except for the 'index' action, as/to
# For the 'index' action I would like to use plural 'users' instead of 'user'
http://<site_name>/users/accounts
# and
http://<site_name>/users
How to do that?
I tryed this
namespace "users", :path => "user", :except => :index do
resources :accounts
end
but it doesn't work.
try this
namespace "users", :path => "user" do
resources :accounts, :except => :index
end
I have a widget that has many links. In the routes file:
map.resources :widgets, :has_many => [:links]
I want to add a "sort" action to links. What do I need to add to the routes file, so I can write sort_widget_links_path(widget)
You can define it using a block:
map.resources :widgets do |widget|
widget.resources :links, :member => {:sort => :get}
end
map.resource :basket, :collection => { :checkout => :post }
The above does not work for a resource, as you would expect since basket is a resource (ie. singular) not resources, so there is no concept of a collection, everything should be scoped to the current_user. In this case User has_one Basket.
However I would like to specify a custom route without having to resort to adding another line in routes, eg:
map.checkout 'basket/checkout', :controller => 'baskets', :action => 'checkout'
Is this possible?
Of course my other option is to add a checkouts controller.
Just use :member option instead of :collection:
map.resource :basket, :member => {:checkout => :post}
If the basket is scoped to the user I'd make it a nested resource:
map.resources :users do |users|
users.resource :basket, :member => { :checkout => :post }
end
... or in Rails 3 ...
resources :users do
resource :basket do
post :checkout, :on => :member
end
end
This way you'll be able to scope the basket to the user who's checking out. The URL will end up looking like this:
/users/5/basket/checkout
You also get the nicely worded named route 'checkout_user_basket'.