2011-03-20 153 views
0

我在继续学习Rails 3的过程,但路由让我疯狂。我正尝试使用名称空间来分隔网站的管理部分。问题是命名空间中的某些内容根本无法正常工作,并且还会路由到错误的地方。例如,使用rails通过指定资源生成路由时,视图在传递对象时指向错误的路由,因此编辑表单不起作用。命名空间路由导致恶梦

即使路线确实存在,它与link_to的链接也不起作用。首先这里是从rake路由输出的命名空间路由。

namespace :admin do 
    resources :users 
end 


admin_users GET  /admin/users(.:format)   {:action=>"index", :controller=>"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 PUT /admin/users/:id(.:format)  {:action=>"update", :controller=>"admin/users"} 
       DELETE /admin/users/:id(.:format)  {:action=>"destroy", :controller=>"admin/users"} 

控制器:

class Admin::UsersController < ApplicationController 

    def index 
    @users = User.all 
    end 

    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     redirect_to(@user, :notice => 'User was successfully created.') 
    else 
     render :action => "new" 
    end 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(params[:user]) 
     redirect_to(admin_users_path, :notice => 'User was successfully updated.') 
    else 
     render :action => "edit" 
    end 
    end 

    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 
    redirect_to(admin_users_path) 
    end 
end 

实施例视图:index.html.erb列出所有用户

<h1>Listing users</h1> 

<table> 
<% for user in @users %> 
    <tr> 
    <td><%= user.id %></td> 
    <td><%= user.username %></td> 
    <td><%= user.email %></td> 
    <td><%= link_to 'Show', @user %></td> 
    <td><%= link_to 'Edit', edit_admin_user_path(user) %></td> 
    <td><%= link_to 'Destroy', admin_user_path, :confirm => 'Are you sure?', :method => :delete %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New User', new_admin_user_path %> 

使用编辑视图也有问题。编辑表单应该指向更新路由,但不是。相反,它只在传递一个User对象时指向编辑路径(基本上就是它自己)。从我一直使用窗体中的对象阅读的东西是推荐的方法,但如果它不起作用,这不是一件好事。

我列出所有用户页面时出现此错误。

No route matches {:action=>"update", :controller=>"admin/users"} 

Extracted source (around line #17): 
17:  <td><%= link_to 'Destroy', admin_user_path, :confirm => 'Are you sure?', :method => :delete %></td> 

我很想努力坚持下去,但是这让我感到疲倦。仅供参考:是的,我知道有认证框架,但我正在尝试从头开始制定认证框架。这是一种学习体验,因此,仅仅使用宝石和插件就不会成为我认为的方式。

谢谢 Onyth

回答

5

你缺少ID的删除链接

尝试用

<td><%= link_to 'Destroy', admin_user_path(user), :confirm => 'Are you sure?', :method => :delete %></td> 

(改变admin_user_pathadmin_user_path(用户)为纽带)

+1

已经工作了,谢谢你,但仍然是一个形式问题。 <%=的form_for:用户做%> 使得 <形式行动= “/管理/用户/ 1 /编辑” 方法= “POST”> 但应/管理/用户/ 1和上放置方法进行更新。如果我在表单标签中使用@user,它只会抛出user_path – pieterk 2011-03-20 11:36:48

+3

尝试'<%= form_for [:admin,@user] do%>' – Teoulas 2011-03-20 13:08:33

+1

奇怪的Rails脚手架如何正确处理名称空间。 – pieterk 2011-03-20 14:15:11

2

如果你做了条耙路线你会看到这样的内容:

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

所以,你可以得到

admin_user_path 

这将是相同的

user_path 

然后,你会通过@user在admin_user_path像这样:

admin_user_path(@user) 

The:方法应该调用destroy方法,而不是自动进入show方法!:)

要得到的form_for工作,我发现了以下资源:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for

下的form_for部分,他们解释命名空间的form_for路由为:

For namespaced routes, like admin_post_url: 

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

有关所耙路线检查出:http://guides.rubyonrails.org/command_line.html#rake-is-ruby-make然后根据部分2.4.9其他任务他们解释耙 - 任务显示您可以使用的各种耙指令和耙线路显示您可用的路线路径。

希望这会有所帮助!