2012-09-17 61 views
0

我在删除和显示用户记录时遇到问题。Ruby on Rails:路由错误

这里是我的routes.rb

FinalApp::Application.routes.draw do 


resources :users 
devise_for :users, :controllers => { :registrations => 'admin' } 



    resources :projects 
    match "search" => "projects#search", :as => :search 

    root :to => 'projects#index' 
end 

这里是我的管理控制器:

class AdminController < ApplicationController 
def index 
    @users = User.all 



respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @users } 
    end 
    end 

def create 

    @user = User.new(params[:user]) 



    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 




    # GET /users/1 
    # GET /users/1.json 
    def show 
    @user = User.find(params[:id]) 
@user_user_id = params[:id] 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/new 
    # GET /users/new.json 
    def new 
    @user = User.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @user } 
    end 
    end 

    # GET /users/1/edit 
    def edit 
    @user = User.find(params[:id]) 
    end 

    # POST /users 
    # POST /users.json 



    # PUT /users/1 
    # PUT /users/1.json 
    def update 
    @user = User.find(params[:id]) 

    respond_to do |format| 
     if @user.update_attributes(params[:user]) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user = User.find(params[:id]) 
    @user.destroy 

    respond_to do |format| 
     format.html { redirect_to users_url } 
     format.json { head :no_content } 
    end 
    end 
end 

这是我的观点:

<%= stylesheet_link_tag "admin" %> 
<body> 
<div id ="title1">Admin</div> 

<div class ="menu"></div> 
<div id ="section3"> 
<table id = "mytable"> 
<table border = "1"> 
<tr> 
    <th>Username </th> 


    <th>Email</th> 
    <th>First Name</th> 
     <th>Last Name</th> 
<th>Admin?</th> 

    <th></th> 
    <th></th> 
    <th></th> 

    </tr> 
    <%= link_to "New User", admin_new_path %><br /> 

<% @users.each do |t| %> 
    <tr> 

    <td><%= t.username %></td> 

    <td><%= t.email %></td> 
    <td><%= t.firstname %></td> 

    <td><%= t.lastname %></td> 
<td><%= t.admin %></td> 


    <td><%= link_to 'Show', t %></td> 
    <td> <%= button_to "Delete", t, method: :delete, data: { confirm: 'Are you sure?' } %></td> 

    </tr> 
<% end %> 
</table></br> 

</body> 

</html> 

我可以显示用户的数据库,但当我去删除一条记录。我得到这个错误No route matches [DELETE] "https://stackoverflow.com/users/11"。我是新手,因此请在试图帮助时记住这一点。提前致谢。

编辑:这里是我的路线=>

admin_index GET /admin(.:format)    admin#index 
         POST /admin(.:format)    admin#create 
       new_admin GET /admin/new(.:format)   admin#new 
       edit_admin GET /admin/:id/edit(.:format)  admin#edit 
        admin GET /admin/:id(.:format)   admin#show 
         PUT /admin/:id(.:format)   admin#update 
         DELETE /admin/:id(.:format)   admin#destroy 
     new_user_session GET /users/sign_in(.:format)  devise/sessions#new 
      user_session POST /users/sign_in(.:format)  devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)  devise/sessions#destroy 
      user_password POST /users/password(.:format)  devise/passwords#create 
     new_user_password GET /users/password/new(.:format) devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 
         PUT /users/password(.:format)  devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)  admin#cancel 
     user_registration POST /users(.:format)    admin#create 
    new_user_registration GET /users/sign_up(.:format)  admin#new 
    edit_user_registration GET /users/edit(.:format)   admin#edit 
         PUT /users(.:format)    admin#update 
         DELETE /users(.:format)    admin#destroy 
       projects GET /projects(.:format)   projects#index 
         POST /projects(.:format)   projects#create 
      new_project GET /projects/new(.:format)  projects#new 
      edit_project GET /projects/:id/edit(.:format) projects#edit 
       project GET /projects/:id(.:format)  projects#show 
         PUT /projects/:id(.:format)  projects#update 
         DELETE /projects/:id(.:format)  projects#destroy 
        search  /search(.:format)    projects#search 
        root  /       projects#index 

EDIT2:这是我的routes.rb文件应该怎么看过。使用耙路我可以改变路径来解决我的问题。

FinalApp::Application.routes.draw do 

    # website home 
    root :to => 'projects#index' 

    # devise sessions (NB does not use admin/users controller) 
    devise_for :users, :controllers => { :registrations => 'users' } 

    # normal controllers 
    resources :users 
    resources :projects 

    # custom routes 
    match "search" => "projects#search", :as => :search 

end 

回答

1

您已经结束了两个航线通往管理#破坏:

DELETE /users(.:format)    admin#destroy 
DELETE /admin/:id(.:format)   admin#destroy 

,但你有相匹配/用户/ XX用户提供了一个可选的格式没有路由,只是它匹配删除一条路由/位。你有一条匹配/ admin/11的路由,所以如果你尝试过它可以工作,但是我会尽量简化一些东西。

你真的需要在设计资源上指定一个控制器吗?你想要在那里重写什么,因为你已经结束了与负载的路线(如取消),导致无处和一些冲突...

尝试一个更简单的路由定义(NB这需要重命名您的AdminController UsersController相反,我会遵守这个约定,因为它会让你的生活更轻松,并匹配您的其他URL,所以你最终用户/ 1等,而不是管理/ 1)

FinalApp::Application.routes.draw do 

    # website home 
    root :to => 'projects#index' 

    # devise sessions (NB does not use admin/users controller) 
    devise_for :users 

    # normal controllers 
    resources :users 
    resources :projects 

    # custom routes 
    match "search" => "projects#search", :as => :search 

end 

那就不要耙路线,以确保你明白路线指向的地方。你需要它说的路线(NB的:ID位):

DELETE /users/:id(.:format)   users#destroy 

或者如果你喜欢一个管理控制器(并愿意理清定制路由)潜水前

DELETE /admin/:id(.:format)   admin#destroy 

也许成这虽然你可以通过轨道路线导向阅读,因为它可能清除一些东西给你:

http://guides.rubyonrails.org/routing.html

+0

我希望用户能够登录,如果他们已被授予管理员身份,他们可以访问网站的管理员部分,在那里可以创建,编辑和删除新用户。我会给你一个建议。谢谢。 – Jazz

+0

谢谢,现在完美。更新我的问题以显示答案。 – Jazz

+0

虽然可以尝试命名空间某些动作/控制器,但我会建议在/ admin下使用一些用户动作而不是名称空间,而只是为某些动作添加冗余检查,以确保只有那些用户才能执行重要动作。随着您的应用程序的增长,您将需要多种限制(由用户状态(管理员,读者,其他?))。看看这个cancan,或者建立你自己的更简单的角色系统。 –

2

您应该添加resources :usersroutes.rb。无论如何,您始终可以在控制台中查看rake routes以查看可用路线。

请注意,您定义路线admin的方式并不完全正确。并非所有东西都是get。例如,创建admin将是post。最简单的方法就是使用像resources :admins这样的东西。

+0

我已经加入我的路线给你看。添加'资源:用户'似乎只是加载了更多的路线,并没有解决问题。 – Jazz

+0

您可能需要将其添加到'devise_for'块之外。 –

+0

我已经移动了资源:devise_for块以上的用户 – Jazz

1

这似乎是Devise的一个非常标准的用法,唯一的区别是Controller的名称。因此,设计路由唯一需要的是:

devise_for :users, :controllers => { :registrations => 'admin' } 

另外,删除所有“get'admin/*'”条目。当您在REST环境中工作时,并非每个HTTP方法都是GET。 Here is one article that discusses REST methods.

+0

我已经在您的建议后更新了我的问题。仍然不能解决我的问题 – Jazz

+0

因为您已经在使用Devise,所以您不需要手动公开:users资源。设计负责你所有的路线生成。因此,您的整个路线文件应该只有一行设计,一行用于根路径,一行用于项目资源,一行用于“搜索”路线匹配。 – nevern02