我遇到了这个问题,我无法确定问题所在。我正在尝试从管理部分添加用户。因此我创建了一个自定义用户控制器,它调用了设计模型。我正在尝试使用自定义表单插入和更新值。
控制器
class Admin::UsersController < ApplicationController
load_and_authorize_resource
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!
layout 'admin-layout'
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation) }
end
def index
@user = User.all
end
def new
@user = User.new
end
def create
@user = User.new
if @user.save
redirect_to :action => "index"
else
render :action => "new"
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes
redirect_to :action => "index"
else
render :Action => "update"
end
end
def destroy
User.find(params[:id]).destroy
redirect_to ::Action => "index"
end
end
查看档案
<div class="container">
<div class="col-md-6 col-md-offset-2">
<h2>Create New User</h2>
<%= form_for @user, url: {action: "create"} do |f| %>
<%= f.text_field :email, :class=>"form-control", :placeholder => "Enter Email", :required => "true" %>
<%= f.text_field :password, :class=>"form-control", :placeholder => "Enter Password", :required => "true" %>
<%= f.text_field :password_confirmation, :class=>"form-control", :placeholder => "Enter Password Again", :required => "true" %>
<p>Roles:</p>
<% for r in Role.all %>
<%= check_box_tag "user[role_ids][]", r.id %>
<%= r.name %>
<% end %><br/>
<%= f.submit "Create", :class=>"btn btn-primary" %>
<% end %>
</div>
<div>
路线
admin_users GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
任何人都可以指出我在哪里弄错了。
答案 0 :(得分:4)
您将new
,create
等方法定义为protected
,如果您希望将它们识别为操作,则需要公开这些方法。
Role.all
,那么使用each
代替for
(这里很少使用)来迭代它会更具可读性:
<% Role.all.each do |r| %>