无法更新设计对象

时间:2014-07-08 22:33:13

标签: ruby-on-rails devise

我使用的是rails 4,我遇到了允许的参数问题。我让他们为创作工作,但没有更新......

我有一个有趣的设计设计。我有两个不同的设计模型,一个通过数据库关联属于另一个。

我的问题是,当我去更新时,它会将我发回编辑页面,它说请查看下面的问题,并且没有任何突出显示,因为错误会显示。

服务器看起来像这样,但我没有看到错误......

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"=>"patrick@shiftedrecording.com", "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"=>"patrick@shiftedrecording.com", "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)

我甚至没有看到提交......我也不确定为什么会有这么多的重复,但这不是我的问题(除非是)

首先 - &gt; application_controller.rb

before_filter :configure_permitted_parameters, if: :devise_controller?

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

这应该适用于BOTH模型,就像我的application_controller ...

一样

接下来,我覆盖了客户端/ 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

我会发布我的路线以获得良好的衡量标准

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 

为什么我的资源没有更新?!

1 个答案:

答案 0 :(得分:1)

IIRC,您需要传递一个名为current_password的参数,以便通过RegistrationsController更新用户。