Reputation: 1560
I'm using rails 4, and I was having issues with the permitted parameters. I got them working for create, but not update...
I have an interesting set-up with devise. I have two different devise models, and one belongs to another via database association.
My problem is that when I go to update, it sends me back to the edit page, it says please review the problems below, and there is nothing highlighted as an error would indicate.
The server looks like this, but I don't see an error...
Started PUT "/clients" for 127.0.0.1 at 2014-07-08 18:19:14 -0400
Processing by Client::RegistrationsController#update as HTML
Processing by Client::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jHJRAX3ML8AYEGbwCDYcsbuFbvPUk5RDQEK87LIQObA=", "client"=>{"first_name"=>"Kanye", "last_name"=>"West", "rate"=>"2000", "email"=>"[email protected]", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000106883658 @tempfile=#<Tempfile:/var/folders/dp/9ll62dd16fn75857f5_8kf_m0000gn/T/RackMultipart20140708-14658-3l4eyr>, @original_filename="kanye.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"client[image]\"; filename=\"kanye.jpg\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "engineer_id"=>"2"}, "commit"=>"Update"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jHJRAX3ML8AYEGbwCDYcsbuFbvPUk5RDQEK87LIQObA=", "client"=>{"first_name"=>"Kanye", "last_name"=>"West", "rate"=>"2000", "email"=>"[email protected]", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000106883658 @tempfile=#<Tempfile:/var/folders/dp/9ll62dd16fn75857f5_8kf_m0000gn/T/RackMultipart20140708-14658-3l4eyr>, @original_filename="kanye.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"client[image]\"; filename=\"kanye.jpg\"\r\nContent-Type: image/jpeg\r\n">, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "engineer_id"=>"2"}, "commit"=>"Update"}
Client Load (0.7ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = 16 ORDER BY "clients"."id" ASC LIMIT 1
Client Load (0.7ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = 16 ORDER BY "clients"."id" ASC LIMIT 1
Client Load (0.5ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT 1 [["id", 16]]
Client Load (0.5ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT 1 [["id", 16]]
Engineer Load (0.3ms) SELECT "engineers".* FROM "engineers" WHERE "engineers"."id" = $1 LIMIT 1 [["id", 2]]
Engineer Load (0.3ms) SELECT "engineers".* FROM "engineers" WHERE "engineers"."id" = $1 LIMIT 1 [["id", 2]]
Engineer Load (0.3ms) SELECT "engineers".* FROM "engineers"
Engineer Load (0.3ms) SELECT "engineers".* FROM "engineers"
Rendered clients/registrations/edit.html.erb within layouts/application (13.9ms)
Rendered clients/registrations/edit.html.erb within layouts/application (13.9ms)
Studio Load (0.4ms) SELECT "studios".* FROM "studios" ORDER BY "studios"."id" ASC LIMIT 1
Studio Load (0.4ms) SELECT "studios".* FROM "studios" ORDER BY "studios"."id" ASC LIMIT 1
Rendered partials/_header.html.erb (2.4ms)
Rendered partials/_header.html.erb (2.4ms)
Completed 200 OK in 200ms (Views: 68.6ms | ActiveRecord: 2.2ms)
I don't even see a commit...I'm also not certain why there is so much duplication but that's not really my issue (unless it is)
First -> application_controller.rb
before_filter :configure_permitted_parameters, if: :devise_controller?
and
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :first_name, :last_name, :rate, :engineer_id, :image) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :first_name, :last_name, :rate, :engineer_id, :image) }
end
This should apply to BOTH models, as it is in my application_controller...
Next, my overridden clients/registration_controller
class Client::RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication, :only => [:new, :create]
def create
build_resource(sign_up_params)
resource_saved = resource.save
yield resource if block_given?
if resource_saved
if resource.active_for_authentication?
flash[:success] = "New Client Added"
redirect_to root_path
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
resource_updated = update_resource(resource, account_update_params)
yield resource if block_given?
if resource_updated
if is_flashing_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
sign_in resource_name, resource, bypass: true
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
end
end
end
I'll post my routes for good measure
devise_for :clients, :controllers => {:registrations => "client/registrations"}
resources :clients, :only => [:show, :new, :edit, :update]
devise_for :engineers, :controllers => {:registrations => "engineer/registrations"}
resources :engineers, :only => [:show]
devise_scope :client do
get "clients/:id", to: "clients#show"
end
Why doesn't my resource update?!
Upvotes: 0
Views: 54
Reputation: 29880
IIRC, you need to pass a parameter called current_password
in order to update a user via RegistrationsController.
Upvotes: 1