5

I created a routing test rails3 app with one model 'User':

rails new routing_test_app
rails generate model User name:string
rails generate scaffold_controller admin/user
rake db:migrate

Added to routes.db:

namespace :admin do
  resources :users
end

rake routes

admin_users GET    /admin/users(.:format)          {:action=>"index", :controller=>"admin/users"}
admin_users POST   /admin/users(.:format)          {:action=>"create", :controller=>"admin/users"}
new_admin_user GET    /admin/users/new(.:format)      {:action=>"new", :controller=>"admin/users"}
edit_admin_user GET    /admin/users/:id/edit(.:format) {:action=>"edit", :controller=>"admin/users"}
admin_user GET    /admin/users/:id(.:format)      {:action=>"show", :controller=>"admin/users"}
admin_user PUT    /admin/users/:id(.:format)      {:action=>"update", :controller=>"admin/users"}
admin_user DELETE /admin/users/:id(.:format)      {:action=>"destroy", :controller=>"admin/users"}

views/admin/users/_form.html.erb

<%= form_for(@admin_user) do |f| %>
  <% if @admin_user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@admin_user.errors.count, "error") %> prohibited this admin_user from being saved:</h2>

      <ul>
      <% @admin_user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
    <%= f.text_field :name %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When I go to 'http://localhost:3000/admin/users/new' rails throws an error:

undefined method `users_path' for #<#<Class:0x0000010116ca90>:0x000001011588d8>

Extracted source (around line #1):

1: <%= form_for(@admin_user) do |f| %>
2:   <% if @admin_user.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@admin_user.errors.count, "error") %> prohibited this admin_user from being saved:</h2>

2 Answers 2

5

That's because @admin_user is a User object, so Rails guesses the URL helper to be users_path. It's a simple fix. Just replace @admin_user form_for param with [:admin, @admin_user]. You might also want to rename the instance variable @user for less repitition. Having to use the array is a drawback of using namespaces, so one should always take that into consideration.

Sign up to request clarification or add additional context in comments.

Comments

4

The form_for method will not guess nested routes. Try this:

form_for [:admin, @admin_user] do |f|

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.