そのため、管理フォームが機能しており、ユーザーがリストされています。ただし、ユーザーの更新に関しては大きな問題があります。次のエラー トレースが表示されます。
ID=User2 のユーザーが見つかりませんでした
以下は、管理者用のユーザーコントローラーです。更新領域を見ています。
class UsersController < ApplicationController
before_filter :authenticate_user!
def index
authorize! :index, @user, :message => 'Not authorized as an administrator.'
@users = User.all
@books = Book.where(approved: false)
@snippets = Snippet.where(approved: false)
end
def show
@snippet = Snippet.where(approved: false)
@user = User.find(params[:id])
@book = Book.where(approved: false)
end
def update
authorize! :update, @user, :message => 'Not authorized as an administrator.'
@user = User.find(params[:id])
@role = Role.find(params[:user][:role_ids]) unless params[:user][:role_ids].nil?
params[:user] = params[:user].except(:role_ids)
if @user.update_attributes(params[:role_id])
@user.update_plan(role) unless role.nil?
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
authorize! :destroy, @user, :message => 'Not authorized as an administrator.'
user = User.find(params[:id])
unless user == current_user
user.destroy
redirect_to users_path, :notice => "User deleted."
else
redirect_to users_path, :notice => "Can't delete yourself."
end
end
end
ここで何をすればいいのか、他にどのような情報が必要なのかわかりません。私はまだこのコードに取り組んでおり、これは私のプレイアラウンドアプリであり、非常に新しく、まだ新しいことを学んでいますが、ロールの更新について頭を悩ませていただければ幸いです。attr_accessible は role_id ですべて正しく、role_id はユーザー テーブルにありますが、以下のように適切な場所に移動するとは思いません。
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"GCnVzWUzSAwb9f0oxSYrAW/ZiuoZg4Yh2qULFiNe+Ls=",
"user"=>{"role_ids"=>"3"},
"commit"=>"Change Role",
"id"=>"User2"}
私はとても混乱しています。
_更新に使用するフォーム:
<div id="role-options-<%= user.id %>" class="modal" style="display: none;">
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Change Role</h3>
</div>
<div class="modal-body">
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids.first %>
</div>
<div class="modal-footer">
<%= f.submit "Change Role", :class => "btn" %>
<a class="btn" data-dismiss="modal" href="#">Close</a>
</div>
<% end %>
</div>
ユーザーモデル:
class User < ActiveRecord::Base
rolify
after_create :assign_default_role
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
def assign_default_role
add_role(:clerk)
end
belongs_to :role
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :profile_name, :password,
:password_confirmation, :remember_me, :approved, :role_id
def setup_default_role_for_new_users
self.roles = [ "default" ]
end
validates :first_name, presence: true
validates :last_name, presence: true
validates :profile_name, presence: true,
uniqueness: true,
format: {
with: /^[a-zA-Z0-9_-]+$/,
message: 'Must be formatted correctly.'
}
has_many :books
has_many :snippets
has_many :statuses
has_many :user_friendships
has_many :friends, through: :user_friendships,
conditions: { user_friendships: { state: 'accepted' } }
has_many :pending_user_friendships, class_name: 'UserFriendship',
foreign_key: :user_id,
conditions: { state: 'pending' }
has_many :pending_friends, through: :pending_user_friendships, source: :friend
def full_name
name = "#{first_name.capitalize} #{last_name.capitalize}"
end
def to_param
profile_name
end
def gravatar_url
stripped_email = email.strip
downcased_email = stripped_email.downcase
hash = Digest::MD5.hexdigest(downcased_email)
"http://gravatar.com/avatar/#{hash}"
end
end
コンソール出力:
私はこれを正しく読んでいますか、user_id を 4 に変更しているようです。
Processing by UsersController#index as HTML
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1←[0m
←[1m←[35m (0.0ms)←[0m SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "us
ers_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" ←[0m
←[1m←[35mRole Load (0.0ms)←[0m SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 1 LIMIT 1
←[1m←[36mCACHE (0.0ms)←[0m ←[1mSELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 1 LIMIT 1←[0m
←[1m←[35mRole Load (0.0ms)←[0m SELECT "roles".* FROM "roles"
←[1m←[36m (0.0ms)←[0m ←[1mSELECT "roles".id FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHE
RE "users_roles"."user_id" = 1←[0m
Rendered users/_user.html.erb (4.0ms)
←[1m←[35mRole Load (1.0ms)←[0m SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 2 LIMIT 1
←[1m←[36mCACHE (0.0ms)←[0m ←[1mSELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 2 LIMIT 1←[0m
←[1m←[35mCACHE (0.0ms)←[0m SELECT "roles".* FROM "roles"
←[1m←[36m (0.0ms)←[0m ←[1mSELECT "roles".id FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHE
RE "users_roles"."user_id" = 2←[0m
Rendered users/_user.html.erb (4.0ms)
←[1m←[35mRole Load (0.0ms)←[0m SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 3 LIMIT 1
←[1m←[36mCACHE (0.0ms)←[0m ←[1mSELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 3 LIMIT 1←[0m
←[1m←[35mCACHE (0.0ms)←[0m SELECT "roles".* FROM "roles"
←[1m←[36m (1.0ms)←[0m ←[1mSELECT "roles".id FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHE
RE "users_roles"."user_id" = 3←[0m
Rendered users/_user.html.erb (4.0ms)
←[1m←[35mRole Load (0.0ms)←[0m SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 4 LIMIT 1
←[1m←[36mCACHE (0.0ms)←[0m ←[1mSELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id"
WHERE "users_roles"."user_id" = 4 LIMIT 1←[0m
←[1m←[35mCACHE (0.0ms)←[0m SELECT "roles".* FROM "roles"
←[1m←[36m (0.0ms)←[0m ←[1mSELECT "roles".id FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHE
RE "users_roles"."user_id" = 4←[0m
Rendered users/_user.html.erb (5.0ms)
←[1m←[35mBook Load (1.0ms)←[0m SELECT "books".* FROM "books" WHERE "books"."approved" = 'f'
Rendered books/_book.html.erb (1.0ms)
←[1m←[36mSnippet Load (0.0ms)←[0m ←[1mSELECT "snippets".* FROM "snippets" WHERE "snippets"."approved" = 'f'←[0m
Rendered users/index.html.erb within layouts/application (34.0ms)
←[1m←[35mCACHE (0.0ms)←[0m SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHER
E "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
←[1m←[36m (0.0ms)←[0m ←[1mSELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE
"users_roles"."user_id" = 1 AND (((roles.name = 'editor') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))←[0m