2

has_many :through アソシエーションを保存するフォームの作成に問題があります。json を投稿して保存に成功しましたが、フォームはまだ機能しません。フォームの送信によって作成されたリクエスト パラメータは機能しません。解決策を教えてくれる助けがあれば、これ以上時間を失うことはありません。前もって感謝します。

編集済み -- 追加の forms_for 試行と、作成された params json が下部で同様に機能しない --

動作する Json 投稿リクエスト パラメータ:

{
    "author": {
        "name": "Author Name",
        "post_authors_attributes": [
          {"post_id":"1"},
          {"post_id":"2"},
          {"post_id":"3"}
        ]
    }
}

保存しない Rails フォーム生成パラメータ。

{
    "author": {
        "name": "assd",
        "post_authors_attributes": [
            "",
            "2",
            "3"
        ]
    }
}

...および関連するコード サンプル...

著者モデル

class Author < ActiveRecord::Base
  has_many :post_authors
  has_many :posts, :through => :post_authors
  accepts_nested_attributes_for :post_authors
end

投稿モデル(現在、投稿者が多数いる著者のみに取り組んでおり、その逆ではありません)

class Post < ActiveRecord::Base
end

PostAuthor モデル

class PostAuthor < ActiveRecord::Base
  belongs_to :post
  belongs_to :author
end

コントローラーの新規/作成アクションの作成

  # 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

作者/_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 %>

スキーマ

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

編集 - 追加された詳細 - デューデリジェンスのために、fields_for も使用してみましたが、データベースに保存されないさらに混乱した json が生成されます。「0」キーがどこから来たのかわかりません。私はこれにこだわっています。どんな助けでも大歓迎です。

fields_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>

生成されたパラメーター to_json

{
    "author": {
        "name": "test",
        "post_authors_attributes": {
            "0": {
                "post_id": [
                    "",
                    "1",
                    "2",
                    "3"
                ]
            }
        }
    }
}
4

2 に答える 2

4

同じ種類の問題に取り組んでいる人のために、私は最終的に次の collection_select でこれを機能させることができました:

      <%= f.collection_select(:feature_ids, Feature.all, :id, :name,
                              {include_blank: false, :include_hidden => false, :selected => @property.features.map(&:id)},
                              {:multiple => true}) %>
于 2015-01-24T17:48:25.547 に答える