Rails 4 simple_form has_many 通过复选框不保存

2024-02-06

我正在尝试使用 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

form

<%= 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" = '[email protected] /cdn-cgi/l/email-protection' 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)

我需要以下强参数

def employee_params
  params.require(:employee).permit(:first_name, :recurrent_ids => [])
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails 4 simple_form has_many 通过复选框不保存 的相关文章

  • 从架构中删除表 - Rails

    我想删除架构中的一个表 我在第一次启动项目时创建了数据库并希望删除该表 这样做的最佳方法是什么 I tried rails g migration drop table installs但这只会创建一个空迁移 Schema create t
  • 如何在测试环境中通过 URL 访问 ActiveStorage 对象?

    给定一个具有 ActiveStorage 附件的模型 class MyObject has one attached avatar end 在开发环境中 我能够将头像作为 StringIO 对象检索 obj MyObject new val
  • 如何设置管理员批准模型的编辑

    我需要一个普通用户可以编辑模型的系统 但编辑实际上只有在管理员批准后才会发生 我发现了一颗宝石 叫做纸迹 https github com airblade paper trail它确实有模型版本控制 但不具体支持我想要做的事情 我想知道其
  • 我在 apache 中使用乘客 for Rails 时收到 403 错误

    我已经安装了所需的工具 并遵循了几个教程 试图让乘客做出回应 我可以访问公共文件夹 public 500 html 或 422 hml 中的静态文件 昨天我通过虚拟主机进入 发现一些乘客错误 但一段时间后 托管重新启动了服务 从那时起我就无
  • 防止语言环境文件中的 HTML 字符实体被 Rails3 xss 保护破坏

    我们正在构建一个应用程序 这是我们第一个使用 Rails 3 的应用程序 并且我们必须从一开始就构建 I18n 作为完美主义者 我们希望在我们的视图中使用真正的排版 破折号 卷曲引号 省略号等 这意味着在我们的 locales xx yml
  • 如何在 Ruby on Rails 3 中使用 Active Record 搜索文本?

    如何在 Ruby on Rails 中搜索字符串 例如 列包含 文本 的所有记录 Active Record 有方法吗 还是我必须使用 SQL LIKE Model find all conditions gt name LIKE tag
  • RubyMine 不能使用 Guard 吗?

    由于某些无法解释的原因 RubyMine 会自动保存您所做的每一个更改 因此每次击键都会触发 Guard 运行您的测试 最可笑的是 显然没有办法禁用这个自动保存 功能 我只是想知道 RubyMine 似乎是 Rails 开发人员中非常流行的
  • 使用 SASS 切换用户主题 - Ruby on Rails

    所以我有一个 Rails 管理系统 允许用户选择一个主题 基本上是一组 SASS 颜色变量 它将使用新颜色重新编译 application css scss 当用户从下拉菜单中选择并提交时 更改此设置的最佳方法是什么 我阅读了一些有关缓存和
  • Rails 3.1 和 Asset Pipeline:使用 Capistrano 进行部署时遇到的问题

    我刚刚从 Ruby on Rails 3 0 10 切换到 3 1 0 我想使用 Capistrano gem 部署我的资产文件 我的本地计算机是运行 Snow Leopard 的 MacO 我的远程计算机运行的是 Ubuntu 10 04
  • 当 config.cache_classes = true 时缺少方法

    我有两个名为 Scope 和 ScopeItem 的 ActiveRecord 模型 具有 has many 关系 class Scope lt ActiveRecord Base has many scope items end clas
  • Facebook API 错误 100 - 无效链接

    我正在使用 Facebook API 在我的 Rails 应用程序中创建发送对话框 我只是在 Javascript 中使用 Facebook 推荐的格式 作为 HTML 中的脚本 我的问题是我得到 API Error code 100 in
  • Googlebot 收到现有模板缺少模板错误

    在过去的几天里 当谷歌机器人尝试访问我们的主页 欢迎 索引 时 我们开始收到缺少模板的错误 我已经盯着这个看了几个小时 知道我只是错过了一些简单的东西 A ActionView MissingTemplate occurred in wel
  • rvm编译安装ruby 2.5.0出错

    我正在尝试使用 rvm 安装 ruby 2 5 0 但出现错误 我在 Ubuntu 18 16 和现在的 Linux Mint Cinnamon 上尝试过 基本上我在运行安装 ruby 的代码之前所做的是 打开 GPG 密钥https rv
  • 如何阻止与 RSpec 和 Capybara 的外部连接?

    在我的 Rails 项目中 我想编写非理想条件的测试 例如缺乏互联网连接或超时 例如 我正在使用 gem 来联系 API 并且希望确保在我的应用程序和外部 API 之间存在连接问题时能够正确处理错误 我已经可以通过用录像机制作固定装置并从
  • MONGODB [DEBUG] 游标的cursor.refresh() 7078636577051629992

    更新大型 json 列表时 出现以下错误 2012 04 01T09 34 00 00 00 app run 1 MONGODB DEBUG cursor refresh for cursor 7078636577051629992 201
  • Rails 中的 PDF 导出

    我需要将包含一些图表的 HTML 页面导出为 PDF 有哪些好的 gem 可以做到这一点 PDFKit http railscasts com episodes 220 pdfkit http railscasts com episodes
  • 预期的 ProductField,出现数组问题

    我有一个 Rails 4 应用程序 它有一个如下所示的 params 块 def store params params require store permit name description user id products attr
  • git push heroku master 权限被拒绝

    我正在关注 ruby railstutorial 我运行命令 git push heroku master 它吐出了这个错误 Permission denied publickey fatal Could not read from rem
  • 在rails中,如何将记录作为csv文件返回

    我有一个名为 Entries 的简单数据库表 class CreateEntries lt ActiveRecord Migration def self up create table entries do t t string firs
  • Rails 3 + angularjs + 缩小在生产中不起作用:未知提供者:eProvider

    我已遵循我能找到的所有修复缩小的说明 例如 var MyController function renamed scope renamedGreeter MyController inject scope greeter and someM

随机推荐