So in Rails2 you could setup a route like this:
resources :users, :as => 'members'
This way you still have your users_path but it matches /members
Now, how do you go about doing this in Rails3? :as parameter here means totally different thing and I can't find what makes it work as before.
I think what you're looking for is the :path option:
resources :users, :path => 'members'
Tested it on my app and the users_path still works, but matches /members as required. It doesn't match /users.
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
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
I keep getting strange errors because of the way my routes.rb file is organized. The latest one is that some function cannot find action "show" in model Relations controller (the action is obviously there). I guess this is because I am adding some custom actions via collection and something about the order in which the routes are declared is messed up.. Can somebody please have a look at this and say what is wrong?
YApp::Application.routes.draw do
require 'resque/server'
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
match '/get_idx', :to => 'nodes#get_idx'
resource :relations do
collection do
post 'this_relation'
post "iframize"
end
end
resource :words do
get 'page/:page', :action => :index, :on => :collection
collection do
get 'front'
get 'index'
end
end
resource :recommendations do
collection do
get 'find_votes'
end
end
get "connotation/create"
get "connotation/edit"
get "connotation/update"
root :to => "words#front", :as => :homepage
resources :users, :user_sessions, :relations, :evaluation, :phrases, :metawords, :nodes, :recommendations, :words
mount Resque::Server.new, :at => "/resque"
match 'about' => 'words#index' , :as => :about
match 'contact' => 'keywords#index' , :as => :contact
end
You might have an issue with resource :relations. Rule of thumb is: if you use the plural resources, then the name of the resource must also be plural (i.e. :relations), if you use resource, in singular, than you should use singular for the resource name too (i.e. :relation).
Other possible problems: your indentation is off. Maybe it's just a copy-paste issue, but check it nonetheless, because you might have some unexpected nesting going on.
Also inspect rake routes CONTROLLER=relations. Compare that to the log of the failed request and see if every parameter matches up.
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!
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.