views:

365

answers:

2

I have a User model. If I do:

def my_action
  @user = User.new
end

then

  <% form_for(@user) do |f| %>

I get

undefined method `users_path' for #<ActionView::Base:0x1b4b878>

Which make sense because I haven't mapped it going map.resources :users... but I don't want to do it this way because I don't need all the resources.

How can I just define this user_path method in my routes?

+2  A: 

You can map custom routes in your routes.rb file like this...

map.users '/users', :controller => 'user', :action => 'index'

This gives you the users_path helper you're looking for.

Jason Punyon
Thanks. Is this a convention? Like the route to the index is model_path ?
marcgg
This is incorrect. This won't solve the form posting cos it's going to the wrong action. map.resources :users, :only =>[:new, :create] is the appropriate convention.
Brian Hogan
+2  A: 

You can also customize restful routes. For example in my application only the index and show actions are appropriate for certain controllers. In my routes.rb file I have some routes like this:

map.resources :announcements, :only => [:index, :show]

You can also use :except if that's more appropriate.

Andy Gaskell