Namespace route and form_for method

When you have a namespace in your routes.rb like this:

  map.namespace :admin do |admin|
    admin.resources :users
  end

And you like to have a form like this:

<% form_for(@user) do |f| %>
  <%= f.error_messages %>

Change the form_for in this and you’re good to go!

<% form_for([:admin, @user]) do |f| %>
  <%= f.error_messages %>

This might save you some time. ;-)

This entry was posted in Ruby on Rails and tagged , , , . Bookmark the permalink.

5 Responses to Namespace route and form_for method

  1. Geoffroy says:

    Thx for the tip!
    I was replacing the form_for(@user) with form_for(@user, :url => admin_user_path(@user))…
    Your way is shorter and cleaner.

    Geoff

  2. celio motta says:

    Great!!! Thank you!!!

  3. naroz says:

    thanks, you saved my day

  4. Thank you for the fast tip!!

  5. Jesse says:

    Thanks! That’s a lot easier than how I was doing it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>