所以我的管理员表单正常工作,用户列在其中。但是,在更新用户方面,我遇到了很多问题。我得到以下错误跟踪。
找不到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
我不确定我需要在这做什么以及您需要什么其他信息。我还在研究这个代码,这是我的应用程序,非常新的仍然仍在学习新的东西,但真的很感激我的头脑更新角色。使用role_id时attr_accessible是正确的,而role_id在用户表中是正确的,但我不认为它使用以下内容将我送到正确的位置:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"GCnVzWUzSAwb9f0oxSYrAW/ZiuoZg4Yh2qULFiNe+Ls=",
"user"=>{"role_ids"=>"3"},
"commit"=>"Change Role",
"id"=>"User2"}
我很困惑。
_Form用于更新:
<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
答案 0 :(得分:0)
而不是
@user = User.find(params[:id])
你应该:
@user = User.find_by_profile_name!(params[:id])
您遇到此错误,因为您在params中传递profile_name,但您尝试按其ID搜索用户。
您还应修改行
@user.update_attributes(params[:role_id])
为:
@user.update_attributes(params[:user])
答案 1 :(得分:-1)
在您的更新操作中尝试更改此内容:
@user = User.find(params[:user_id])
到此:
@user = User.find(params[:id])
线索出现在您的错误中:
Couldn't find User with id=User2