ActiveAdmin更新记录未使用正确的主键

时间:2014-09-15 12:58:25

标签: ruby-on-rails ruby activeadmin

我遇到了ActiveAdmin没有更新我的记录的问题,我在日志中注意到更新语句正在传递" 0"的PK密钥。而不是" 5"的params [:id] ... ...我知道如何覆盖它?或任何解决方法?我想也许这可能是因为我没有使用正常的PK" id" ..有什么想法吗?提前谢谢......

Started PATCH "/admin/doctors/5" for 127.0.0.1 at 2014-09-15 20:51:35 +0800
Processing by Admin::DoctorsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"rkmUILbx/t6Rk5jtanupJkt5yZBmj6PbGQhnhxcOP0U=", "doctor"=>{ "gender"=>"0", "details"=>"asdfasdf", "active_flag"=>"Y"}, "commit"=>"Update Doctor", "id"=>"5"}
  AdminUser Load (0.4ms)  SELECT  `admin_users`.* FROM `admin_users`  WHERE `admin_users`.`id` = 2  ORDER BY `admin_users`.`id` ASC LIMIT 1
  Doctor Load (0.2ms)  SELECT  `doctors`.* FROM `doctors`  WHERE `doctors`.`doctor_id` = ? LIMIT 1  [["doctor_id", 5]]
  SQL (0.7ms)  BEGIN
  SQL (0.3ms)  UPDATE `doctors` SET `gender` = ?, `updated_at` = ? WHERE `doctors`.`doctor_id` = 0 [["gender", 0], ["updated_at", "2014-09-15 12:51:35"]]
   (0.6ms)  COMMIT
Redirected to http://localhost:3000/admin/doctors/5
Completed 302 Found in 40ms (ActiveRecord: 2.2ms)


Started GET "/admin/doctors/5" for 127.0.0.1 at 2014-09-15 20:51:35 +0800
Processing by Admin::DoctorsController#show as HTML
  Parameters: {"id"=>"5"}
  AdminUser Load (0.3ms)  SELECT  `admin_users`.* FROM `admin_users`  WHERE `admin_users`.`id` = 2  ORDER BY `admin_users`.`id` ASC LIMIT 1
  Doctor Load (0.3ms)  SELECT  `doctors`.* FROM `doctors`  WHERE `doctors`.`doctor_id` = ? LIMIT 1  [["doctor_id", 5]]
Completed 200 OK in 25ms (Views: 20.9ms | ActiveRecord: 0.6ms)

这里是控制器代码..

ActiveAdmin.register Doctor do

  controller do
    def permitted_params
      params.permit :utf8, :_method, :authenticity_token, :commit, :id, :doctor_id, 
          doctor: [:first_name, :middle_initial, :last_name, :name_suffix, 
                    :title, :primary_spec_id, :dob, :gender, :email, :phone, 
                    :details, :ratings, :active_flag ]
    end

    def update
      params.merge!({doctor_id: params[:id]})
      Rails.logger.debug params.inspect
      super
    end
   end

  menu parent: 'Maintenance Tables'

  scope :is_inactive
  scope :is_active

  filter :first_name
  filter :last_name
  filter :email
  filter :primary_spec_id, :label => 'Specialization' , :as => :select , :collection => Specialization.all.order(name: :asc)
  filter :gender, :as => :select , :collection => { 'Male' => 1, 'Female' => 0 }
  filter :active_flag, :label => 'Status' , :as => :select , :collection => ['Y','N']

  index do
    column :ID, :sortable => :doctor_id, :max_width => "500px" do |doctor|
      link_to(doctor.doctor_id, admin_doctor_path(doctor))
    end
    column :first_name
    column :M_I, :sortable => :middle_initial do |doctor|
      doctor.middle_initial
    end
    column :last_name
    column :suffix, :sortable => :name_suffix do |doctor|
      doctor.name_suffix
    end
    column :title
    column :specialization, :sortable => :primary_spec_id do |doctor|
      specialization = Specialization.find(doctor.primary_spec_id)
      link_to(specialization.name,admin_specialization_path(specialization))
    end
    column :dob
    column :gender, :sortable => :gender  do |doctor|
      gender = ['F','M']
      gender[doctor.gender]
    end
    column :email, :sortable => :email do |doctor|
      mail_to(doctor.email)
    end

    column :phone
    column :active_flag, :sortable => :active_flag do |doctor|
      div :class => "doctor_status" do
        status_tag(doctor.active_flag, ((doctor.active_flag == 'Y') ? :ok : :error))
      end
    end
  end

  form do |f|
    f.inputs "Doctor Details" do
      #f.input :doctor_id
      f.input :first_name
      f.input :middle_initial, :label => 'M.I.'
      f.input :last_name
      f.input :name_suffix, :label => 'Suffix', :as => :select, :collection => ['JR.', 'SR.', 'III', 'IV', 'V']
      f.input :title, :label => 'Specialization Title Prefix', :hint => "(e.g. M.D., etc.)"
      f.input :primary_spec_id, :as => :select,  :collection => Specialization.all.order(name: :asc)
      f.input :dob, :label => 'Birth Date', :hint => "(format: MM/DD/YYYY)"
      f.input :gender, :as => :radio, :collection => { 'M' => 1, 'F' => 0 }
      f.input :email, :as => :email
      f.input :phone, :as => :phone
      f.input :details, :label => 'Doctor\'s Brief Description', :as => :text
      f.input :picture, :label => 'Upload Picture', :as => :file
      f.input :active_flag, :as => :select, :collection => ['Y','N'], :hint => "(Default: Y)"
    end
    f.actions
  end


end

2 个答案:

答案 0 :(得分:0)

如果你允许这样的医生身份,你会得到同样的错误吗?

doctor: [:id, :first_name, :middle_initial, :last_name, :name_suffix, 
                    :title, :primary_spec_id, :dob, :gender, :email, :phone, 
                    :details, :ratings, :active_flag ]

答案 1 :(得分:0)

终于明白了。也许这与ActiveRecord Rails版本4.1.6 ..

有关

所以我尝试将我的宝石恢复到4.0.4

gem 'rails', '4.0.4'

然后它最终正常工作..无论如何感谢..

Started PATCH "/admin/doctors/5" for 127.0.0.1 at 2014-09-16 17:24:19 +0800
Processing by Admin::DoctorsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"I9K+GM6iB1a9OvKvmFrqz3qsf4XwedImLHYihtxYBKY=", "doctor"=>{ "gender"=>"0", "details"=>"asdfasdf", "active_flag"=>"Y"}, "commit"=>"Update Doctor", "id"=>"5"}
  AdminUser Load (3.9ms)  SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 2 ORDER BY `admin_users`.`id` ASC LIMIT 1
  Doctor Load (1.2ms)  SELECT `doctors`.* FROM `doctors` WHERE `doctors`.`doctor_id` = ? LIMIT 1  [["doctor_id", "5"]]
  SQL (0.1ms)  BEGIN
  SQL (0.4ms)  UPDATE `doctors` SET `gender` = ?, `updated_at` = ? WHERE `doctors`.`doctor_id` = 5  [["gender", 0], ["updated_at", Tue, 16 Sep 2014 09:24:19 UTC +00:00]]
   (0.6ms)  COMMIT
Redirected to http://localhost:3000/admin/doctors/5
Completed 302 Found in 33ms (ActiveRecord: 6.3ms)