【问题标题】:Ruby on Rails: Routing errorRuby on Rails:路由错误
【发布时间】:2012-09-17 14:30:53
【问题描述】:

我在删除和显示用户记录时遇到问题。

这是我的路线.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] "/users/11"。我是 Rails 新手,所以在尝试提供帮助时请记住这一点。提前致谢。

编辑:这是我的路线 =>

    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 文件的外观。使用 rake 路由,我能够更改路径来解决我的问题。

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

【问题讨论】:

    标签: ruby-on-rails ruby routing devise


    【解决方案1】:

    您应该将resources :users 添加到您的routes.rb。在任何情况下,您都可以随时在控制台中查看rake routes 以查看可用路由。

    附带说明,您定义admin 路线的方式并不完全正确。并非所有内容都是get。例如,创建admin 将是post。最简单的方法就是使用 resources :admins 之类的东西。

    【讨论】:

    • 我已添加路线供您查看。添加resources :users 似乎只是添加了更多路由,并不能解决问题。
    • 您可能需要将其添加到devise_for 块之外。
    • 我已将资源 :users 移至 devise_for 块上方
    【解决方案2】:

    你最终得到了两条通往 admin#destroy 的路线:

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

    然而你没有匹配 /users/xx 的路由,只有一个匹配 DELETE /users 的路由,带有可选的格式位。您有一条与 /admin/11 匹配的路线,因此如果您尝试它会起作用,但是我会尝试简化一下。

    你真的需要在设计资源上指定一个控制器吗?您究竟想在那里覆盖什么,因为您最终得到了大量路线(如取消),这些路线无处可去,有些路线发生冲突......

    尝试一个更简单的路由定义(注意,这需要重命名您的 AdminController UsersController,我会遵循这个约定,因为它会让您的生活更轻松,并匹配您的其他 url,因此您最终会得到 users/1 等,而不是 admin/ 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
    

    然后执行 rake 路线以确保您了解路线指向的位置。你需要一条路线,上面写着(注意 :id 位):

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

    或者如果您更喜欢管理员控制器(并且愿意整理自定义路由)

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

    也许在深入研究之前,尽管您可以阅读 rails 路由指南,因为它可能会为您清除一些事情:

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

    【讨论】:

    • 我希望用户能够登录,如果他们获得了管理员身份,他们可以进入网站的管理员部分,在那里可以创建、编辑和删除新用户。我会给你一些建议。谢谢。
    • 虽然可以尝试对某些操作/控制器进行命名空间,但我建议您不要为 /admin 下的一些用户操作命名空间,您只需为某些操作添加冗余检查以确保只有这些用户可以执行重要的操作。随着您的应用程序的增长,由于用户状态(管理员、读者、其他?),您将需要多个限制。为此请查看 cancan,或设置您自己的更简单的角色系统。
    【解决方案3】:

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

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

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

    【讨论】:

    • 我根据您的建议更新了我的问题。仍然不能解决我的问题
    • 您不需要手动公开 :users 资源,因为您已经在使用 Devise。设计负责您所有的路线生成。因此,您的整个路由文件应该只有一行用于设计,一行用于根路径,一行用于项目资源,一行用于“搜索”路由匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多