0

我无法删除和显示用户记录。

这是我的路线.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
4

3 回答 3

2

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

附带说明一下,您定义admin路线的方式并不完全正确。并非一切都是get. 例如,创建一个admin将是一个post. 最简单的方法就是使用类似resources :admins.

于 2012-09-17T14:40:14.593 回答
1

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

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

此外,删除所有“get 'admin/*'”条目。在 REST 环境中工作时,并非每个 HTTP 方法都是 GET。这是一篇讨论 REST 方法的文章。

于 2012-09-17T15:27:57.963 回答
1

您最终得到了两条通往 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

于 2012-09-17T20:55:31.440 回答