ruby on rails - Updating User Role -
so have admin form working , users listed on it. i'm having huge issues when comes updating user. following error trace.
couldn't find user id=user2
below users controller admin, i'm looking @ update area.
class userscontroller < applicationcontroller before_filter :authenticate_user! def index authorize! :index, @user, :message => 'not authorized 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 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 update user." end end def destroy authorize! :destroy, @user, :message => 'not authorized 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
i'm not sure need here , other information need. i'm still working on code , play around app, new still , still learning new things appreciate getting head around updating of roles. attr_accessible correct role_id , role_id in user tables don't think sending me right place below:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"gcnvzwuzsawb9f0oxsyraw/ziuozg4yh2qulfine+ls=", "user"=>{"role_ids"=>"3"}, "commit"=>"change role", "id"=>"user2"}
i'm confused.
_form using updating:
<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' } |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>
user model:
class user < activerecord::base rolify after_create :assign_default_role # include default devise modules. others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable , :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 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 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
console output:
am reading right, looks it's changing user_id 4.
processing userscontroller#index html ←[1m←[36muser load (0.0ms)←[0m ←[1mselect "users".* "users" "users"."id" = 1 limit 1←[0m ←[1m←[35m (0.0ms)←[0m select count(*) "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "us ers_roles"."user_id" = 1 , (((roles.name = 'admin') , (roles.resource_type null) , (roles.resource_id null))) ←[1m←[36muser load (0.0ms)←[0m ←[1mselect "users".* "users" ←[0m ←[1m←[35mrole load (0.0ms)←[0m select "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 1 limit 1 ←[1m←[36mcache (0.0ms)←[0m ←[1mselect "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 1 limit 1←[0m ←[1m←[35mrole load (0.0ms)←[0m select "roles".* "roles" ←[1m←[36m (0.0ms)←[0m ←[1mselect "roles".id "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".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 2 limit 1 ←[1m←[36mcache (0.0ms)←[0m ←[1mselect "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 2 limit 1←[0m ←[1m←[35mcache (0.0ms)←[0m select "roles".* "roles" ←[1m←[36m (0.0ms)←[0m ←[1mselect "roles".id "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".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 3 limit 1 ←[1m←[36mcache (0.0ms)←[0m ←[1mselect "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 3 limit 1←[0m ←[1m←[35mcache (0.0ms)←[0m select "roles".* "roles" ←[1m←[36m (1.0ms)←[0m ←[1mselect "roles".id "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".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 4 limit 1 ←[1m←[36mcache (0.0ms)←[0m ←[1mselect "roles".* "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 4 limit 1←[0m ←[1m←[35mcache (0.0ms)←[0m select "roles".* "roles" ←[1m←[36m (0.0ms)←[0m ←[1mselect "roles".id "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".* "books" "books"."approved" = 'f' rendered books/_book.html.erb (1.0ms) ←[1m←[36msnippet load (0.0ms)←[0m ←[1mselect "snippets".* "snippets" "snippets"."approved" = 'f'←[0m rendered users/index.html.erb within layouts/application (34.0ms) ←[1m←[35mcache (0.0ms)←[0m select count(*) "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" wher e "users_roles"."user_id" = 1 , (((roles.name = 'admin') , (roles.resource_type null) , (roles.resource_id null))) ←[1m←[36m (0.0ms)←[0m ←[1mselect count(*) "roles" inner join "users_roles" on "roles"."id" = "users_roles"."role_id" "users_roles"."user_id" = 1 , (((roles.name = 'editor') , (roles.resource_type null) , (roles.resource_id null)))←[0m
instead of
@user = user.find(params[:id])
you should have:
@user = user.find_by_profile_name!(params[:id])
you have error because pass profile_name in params, try search user id.
you should modify line
@user.update_attributes(params[:role_id])
to:
@user.update_attributes(params[:user])
Comments
Post a Comment