我无法删除和显示用户记录。
这是我的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] "/users/11"
。我是rails的新手,所以请在尝试帮助时记住这一点。提前谢谢。
编辑:这是我的路线=&gt;
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
答案 0 :(得分:2)
您应该将resources :users
添加到routes.rb
。无论如何,您始终可以在控制台中查看rake routes
以查看可用路线。
另外,您定义admin
路线的方式并不完全正确。并非一切都是get
。例如,创建admin
将是post
。最简单的方法就是使用类似resources :admins
的内容。
答案 1 :(得分:1)
这似乎是Devise的一个非常标准的用法,你唯一的显着区别是你的控制器名称。因此,设计路由唯一需要的是:
devise_for :users, :controllers => { :registrations => 'admin' }
此外,删除所有“获取'admin / *'”条目。当您在REST环境中工作时,并非每个HTTP方法都是GET。 Here is one article that discusses REST methods.
答案 2 :(得分:1)
你最终得到了两条通往admin#destroy的路线:
DELETE /users(.:format) admin#destroy
DELETE /admin/:id(.:format) admin#destroy
然而你没有匹配/ users / xx的路由,只是一个匹配带有可选格式位的DELETE / users的路由。你有一个匹配/ admin / 11的路由,所以如果你尝试它会工作,但我会尝试简化一些事情。
你真的需要在设计资源上指定一个控制器吗?你究竟想要在那里覆盖什么,因为你最终得到了大量的路线(比如取消),这些路线无处可去而且有些冲突......
尝试更简单的路由定义(NB这需要重命名管理控制器用户控制器,我会遵循这个约定,因为它会让你的生活更轻松,并匹配你的其他网址,所以你最终得到用户/ 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
然后执行rake路线以确保您了解路线指向的位置。你需要一条说(NB:id bit)的路线:
DELETE /users/:id(.:format) users#destroy
或者如果您更喜欢管理员控制器(并且愿意整理自定义路线)
DELETE /admin/:id(.:format) admin#destroy
也许在深入讨论之前,你可以阅读rails路线指南,因为它可能会为你清楚一些事情: