我想通过单击链接将用户切换为管理员。我根据建议更改了代码,但仍然无法正常工作。
控制器:
def toggle_admin
@user = User.find(params[:id])
if @user.is_admin == true
@user.is_admin = false
else
@user.is_admin = true
end
respond_to do |format|
format.html { redirect_to @users }
end
end
视图中调用方法的语句:
<td><%= link_to 'Toggle Admin', :controller => :users, :method => :toggle_admin, :id => user.id, data: { confirm: 'Toggle Admin?' } %></td>
路线:
match 'users/:id/toggle_admin' => 'user#toggle_admin'
有人看到我的错误吗?谢谢。
我认为我的路由可能是问题所在。查看日志如何仅显示所有用户的检索,然后是与 id 匹配的用户:
Started GET "/users?data%5Bconfirm%5D=Toggle+Admin%3F&id=1&method=toggle_admin" for 127.0.0.1 at 2013-07-13 12:16:25 -0400
Processing by UsersController#index as HTML
Parameters: {"data"=>{"confirm"=>"Toggle Admin?"}, "id"=>"1", "method"=>"toggle_admin"}
User Load (36.6ms) SELECT "users".* FROM "users"
User Load (71.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Rendered users/index.html.erb within layouts/application (78.2ms)