2010-12-23 2 views
5

문제 :컨트롤러 네임 스페이스와 라우팅 나는 하나 개의 모델 '사용자'로 라우팅 테스트 rails3 응용 프로그램을 만들어

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

추가는 routes.db합니다 :

namespace :admin do 
    resources :users 
end 

레이크 노선

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"} 

보기/관리자/사용자/_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 %> 

내가 갈 때 '에 http : // localhost를 : 3000/관리자는/사용자/새로운':

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

추출 소스 (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> 

답변

5

users_path가되도록 URL 도우미를 추측. 그것은 간단한 수정입니다. @admin_userform_for 매개 변수를 [:admin, @admin_user]으로 바꿉니다. 인스턴스 변수 @user의 이름을 바꾸면 적은 이름으로 바꿀 수도 있습니다. 배열을 사용하는 것은 네임 스페이스를 사용하는 단점이므로 항상 고려해야합니다.

4
레일에서 오류가 발생합니다

form_for 메서드는 중첩 된 경로를 추측하지 않습니다. 이 시도 : @admin_userUser 객체이기 때문에, 레일 때문이다

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