Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller Namespaces and Routing issue

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>
like image 978
yspro Avatar asked Dec 23 '10 08:12

yspro


People also ask

What is namespace routing?

In its simplest form, a Namespace Routing Language (NRL) schema consists of a mapping from namespace URIs to schema URIs. An NRL schema is written in XML. DSDL Part 4 (ISO/IEC 19757-4), NVDL is based on NRL.

What are routing concerns in Rails?

Routing concerns allow you to declare common routes that can be reused inside other resources and routes.

What are namespaces in Rails?

A namespace is a container for multiple items which includes classes, constants, other modules, and more.


2 Answers

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.

like image 128
dtrasbo Avatar answered Nov 05 '22 10:11

dtrasbo


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

form_for [:admin, @admin_user] do |f|
like image 35
Jimmy Avatar answered Nov 05 '22 09:11

Jimmy