Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a custom path in rails?

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?

like image 490
marcgg Avatar asked Sep 17 '09 01:09

marcgg


People also ask

How do I find a specific route in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What is namespace in Rails routes?

In a way, namespace is a shorthand for writing a scope with all three options being set to the same value.

How do Rails routes work?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


2 Answers

Since I came here looking for a way to create helpers in routes.rb, here is the way to do it:

get '/users/:id/' =>'users#show', :as => :user
like image 82
ironic Avatar answered Sep 23 '22 03:09

ironic


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.

like image 32
Andy Gaskell Avatar answered Sep 26 '22 03:09

Andy Gaskell