【问题标题】:Rails 4 simple_form has_many through check boxes not savingRails 4 simple_form has_many 通过复选框不保存
【发布时间】:2013-08-12 11:02:52
【问题描述】:

我正在尝试通过使用 simple_form 的复选框来实现员工经常性扣减表。我的代码有效,但选定的经常性扣除未保存在我的表中。我不知道为什么。

这是我的模型。

class Employee < ActiveRecord::Base
  belongs_to :club
  has_many :employee_recurrents 
  has_many :recurrents, :through => :employee_recurrents
end

class Recurrent < ActiveRecord::Base
  belongs_to :club
  has_many :employee_recurrents 
  has_many :employees, :through => :employee_recurrents
end

class EmployeeRecurrent < ActiveRecord::Base
  belongs_to :employee
  belongs_to :recurrent 
end

迁移

class CreateEmployeeRecurrents < ActiveRecord::Migration
  def change
    create_table :employee_recurrents, :id => false do |t|

      t.references :employee
      t.references :recurrent 

  end

  add_index :employee_recurrents, [:employee_id, :recurrent_id]
  add_index :employee_recurrents, [:recurrent_id, :employee_id]

 end
end

表格

<%= simple_form_for @employee, html: {class: 'form-horizontal' } do |f| %>

  <%= f.error_notification %>

  <%= f.input :first_name %>
  <%= f.input :last_name %>

  <%= f.association :recurrents, 
                     as: :check_boxes, 
                     label_method: :description, 
                     value_method: :id,
                     label: 'Recurrent Deductions' %>

  <%= f.button :submit, class: 'btn btn-primary' %>
<% end %>

控制器

def employee_params
  params.require(:employee).permit(:first_name, :recurrent_ids)
end

这是我编辑的日志

Started PATCH "/employees/1" for 127.0.0.1 at 2013-08-12 19:38:42 +0800
Processing by EmployeesController#update as HTML
  Parameters: {"utf8"=>"✓",       "authenticity_token"=>"IwWzcT2qQJqHkTumWjb3OD5nJe9xLaA+YezMumer9X8=", "employee"=>{"job_id"=>"5", "manager_id"=>"", "first_name"=>"Mark", "recurrent_ids"=>["1", ""]}, "commit"=>"Update Employee", "id"=>"1"}
  [1m[36mActiveRecord::SessionStore::Session Load (1.2ms)[0m  [1mSELECT "sessions".* FROM "sessions" WHERE "sessions"."session_id" = '9a8b066a4033f8fa2ed867371ffaa2a3' ORDER BY "sessions"."id" ASC LIMIT 1[0m
  [1m[35mUser Load (0.5ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  [1m[36mEmployee Load (0.8ms)[0m  [1mSELECT "employees".* FROM "employees" WHERE "employees"."id" = $1 ORDER BY last_name, first_name LIMIT 1[0m  [["id", "1"]]
Unpermitted parameters: recurrent_ids
  [1m[35m (0.6ms)[0m  BEGIN
  [1m[36mEmployee Exists (1.1ms)[0m  [1mSELECT 1 AS one FROM "employees" WHERE ("employees"."email" = 'mark@yahoo.com' AND "employees"."id" != 1 AND "employees"."club_id" = 1) LIMIT 1[0m
  [1m[35m (0.4ms)[0m  COMMIT
Redirected to http://localhost:3000/employees
Completed 302 Found in 22ms (ActiveRecord: 4.6ms)

【问题讨论】:

  • 服务器登录提交会很有用。对我来说不是必需的,但对其他人来说可能是。 :)
  • 单独拥有f.association :recurrents 有什么作用?它是否有效(即使输入元素类型错误,比如复选框上的下拉菜单)?
  • 我会发布日志,但我看到它显示“未经许可的参数:recurrent_ids”,但我的员工控制器包含 params.require(:employee).permit(:first_name,recurrent_ids)
  • 删除为: :check_boxes 没有帮助。如果我省略 label_method 和 value_method 它只显示一个没有标签的关联哈希。

标签: ruby-on-rails simple-form


【解决方案1】:

我需要以下强大的参数

def employee_params
  params.require(:employee).permit(:first_name, :recurrent_ids => [])
end

【讨论】:

  • 它在文档中,如果一个参数需要一个数组,它必须这样定义。
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 2013-11-24
  • 2012-02-01
  • 1970-01-01
  • 2014-04-22
  • 2013-04-26
  • 2011-08-02
  • 2014-09-22
相关资源
最近更新 更多