Rails has_many :through 与 collection_select 表单助手

Tho*_*mas 2 ruby-on-rails has-many-through collection-select

我在创建一个通过关联保存我的 has_many 的表单时遇到问题。我已经通过发布 json 成功保存了,但表单对我来说还不起作用。表单提交创建的请求参数将无法正常工作。任何帮助我找到解决方案的帮助都会帮助我避免在这方面浪费更多时间。谢谢前面。

编辑 - 添加了 forms_for 尝试和创建的 params json,但在底部效果不佳 -

Json post 请求参数有效:

{
    "author": {
        "name": "Author Name",
        "post_authors_attributes": [
          {"post_id":"1"},
          {"post_id":"2"},
          {"post_id":"3"}
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

Rails 表单生成的参数不保存。

{
    "author": {
        "name": "assd",
        "post_authors_attributes": [
            "",
            "2",
            "3"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

...以及相关的代码示例...

作者模型

class Author < ActiveRecord::Base
  has_many :post_authors
  has_many :posts, :through => :post_authors
  accepts_nested_attributes_for :post_authors
end
Run Code Online (Sandbox Code Playgroud)

帖子模型(目前仅适用于作者有很多帖子,而不是相反)

class Post < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)

帖子作者模型

class PostAuthor < ActiveRecord::Base
  belongs_to :post
  belongs_to :author
end
Run Code Online (Sandbox Code Playgroud)

作者控制器新建/创建操作

  # GET /authors/new
  def new
    @author = Author.new
    @author.post_authors.build
  end

  # POST /authors
  # POST /authors.json
  def create
    @author = Author.new(params)

    respond_to do |format|
      if @author.save
        format.html { redirect_to @author, notice: 'Author was successfully created.' }
        format.json { render :show, status: :created, location: @author }
      else
        format.html { render :new }
        format.json { render json: @author.errors, status: :unprocessable_entity }
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

作者/_form.html.erb

<%= form_for(@author) do |f| %>
  <% if @author.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2>

      <ul>
      <% @author.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

    <%= collection_select(:author, :post_authors_attributes, Post.all, :id, :title,
                                     {include_blank: false, :selected => @author.posts.map(&:id)},
                                     {:multiple => true}) %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

模式

ActiveRecord::Schema.define(version: 20150120190715) do

  create_table "authors", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "post_authors", force: :cascade do |t|
    t.integer  "post_id"
    t.integer  "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end
Run Code Online (Sandbox Code Playgroud)

编辑 - 添加详细信息 - 只是为了尽职调查,我也尝试使用 fields_for,但它会产生更混乱的 json,不会保存到数据库。我不知道“0”键从何而来。我被困在这个问题上,任何帮助将不胜感激。

字段_for

  <div class="field">
    <%= f.fields_for :post_authors, @author.post_authors do |posts_form| %>
        <%= f.label :Posts %><br>
        <%= posts_form.collection_select(:post_id, Post.all, :id, :title,
                                         {include_blank: false, :selected => @author.posts.map(&:id)},
                                         {:multiple => true}) %>

    <% end %>
  </div>
Run Code Online (Sandbox Code Playgroud)

生成参数 to_json

{
    "author": {
        "name": "test",
        "post_authors_attributes": {
            "0": {
                "post_id": [
                    "",
                    "1",
                    "2",
                    "3"
                ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

对于任何遇到同样问题的人,我终于设法让它与以下 collection_select 一起使用:

      <%= f.collection_select(:feature_ids, Feature.all, :id, :name,
                              {include_blank: false, :include_hidden => false, :selected => @property.features.map(&:id)},
                              {:multiple => true}) %>
Run Code Online (Sandbox Code Playgroud)