【问题标题】:Why is the multiple select form_for making my form return nil in rails 4?为什么多重选择 form_for 使我的表单在 rails 4 中返回 nil?
【发布时间】:2014-02-15 11:36:37
【问题描述】:

我的超薄模板如下所示:

= form_for @user_event do |f|
  .field
      = f.select :invited, @friendsArray, {}, { :multiple => true }
  .actions = f.submit 'Save'

@friendsArray example = [["John Nash", "8360775"], ["Jonathan Baldy", "30880087"], ["David Gray", "39900933"]]

当我删除 { :multiple => true } 时,它会提交所选名称的 id。

当我添加 { :multiple => true } 时,我希望它提交多个 ID,但它只为 user_event_params 返回 {}。

【问题讨论】:

    标签: html ruby-on-rails-4 form-for


    【解决方案1】:

    您必须在控制器的 params 方法中允许数组 []。

    在 user_events_controller.rb 我现在有

    def user_event_params params.require(:user_event).permit(invited: []) end

    然后您可能会遇到错误,因为您的数据库可能不允许您将数组保存到其中。

    这可以通过将serialize :invited 添加到您的模型来解决,在我的例子中是 user_event.rb。这很酷,因为当您从数据库中检索属性时,会将其从字符串转回数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多