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?
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.
In a way, namespace is a shorthand for writing a scope with all three options being set to the same value.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With