我正在设置房东管理系统并尝试管理其他用户。我目前在我的用户模型中将admin作为布尔属性,因此我想编辑其他用户,并通过编辑表单将其设为admin。
我正在尝试通过这篇帖子Edit other user as admin in devise, ruby on rails
的影响力来做到这一点这是我的代码:
Routes.rb
devise_for :users, :path_prefix => 'my'
resources :users
视图\用户\编辑
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, required: true, autofocus: true %>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<p>Currently waiting confirmation for: <%= resource.unconfirmed_email %>
</p>
<% end %>
<%= f.input :password,
hint: "leave it blank if you don't want to change it",
required: false,
input_html: { autocomplete: "new-password" } %>
<%= f.input :password_confirmation,
required: false,
input_html: { autocomplete: "new-password" } %>
<%= f.input :admin,
required: false %>
<%= f.input :current_password,
hint: "we need your current password to confirm your changes",
required: true,
input_html: { autocomplete: "current-password" } %>
</div>
<div class="form-actions">
<%= f.button :submit, "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= link_to "Cancel my account",
registration_path(resource_name), data: { confirm: "Are you sure?" },
method: :delete %></p>
控制器\用户
class UsersController < ApplicationController
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to adminpanel_path
else
render 'edit'
end
end
end
问题
如果我转到http://localhost:3000/users/2/edit编辑用户,则会出现以下错误
undefined local variable or method resource_name for #<#<Class:0x8b9df40>:0x8ba7360>
答案 0 :(得分:0)
text/html; charset=utf-8
告诉您resource_name未定义。从您的表单中删除所有资源引用,它将起作用。至于您的取消帐户链接:
undefined local variable or method resource_name for #<#<Class:0x8b9df40>:0x8ba7360>
您需要将resource_name更改为要删除的用户ID。
我还看到您正在使用自定义属性进行设计:
<%= link_to "Cancel my account",
registration_path(resource_name), data: { confirm: "Are you sure?" },
method: :delete %></p>
您允许他们吗?否则,这将是您的下一个错误。如果您不知道该怎么做,请快速评论,我会更新我的答案。
问候!