Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未保存到联接表中的记录具有多个通过关系_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 未保存到联接表中的记录具有多个通过关系

Ruby on rails 未保存到联接表中的记录具有多个通过关系,ruby-on-rails,ruby,Ruby On Rails,Ruby,在这个Rails应用程序中,用户编写故事。用户可以创建集合以对其故事进行分组。然而,他们被允许发表不属于任何收藏的故事 创建故事时,我希望联接表故事集合保存集合/故事ID对,但它不起作用。感谢您的帮助!:) 这是我的 collection.rb class Collection < ActiveRecord::Base belongs_to :user has_many :story_collections has_many :stories, through:

在这个Rails应用程序中,用户编写故事。用户可以创建集合以对其故事进行分组。然而,他们被允许发表不属于任何收藏的故事

创建故事时,我希望联接表故事集合保存集合/故事ID对,但它不起作用。感谢您的帮助!:)

这是我的

collection.rb

class Collection < ActiveRecord::Base

    belongs_to :user
    has_many :story_collections
    has_many :stories, through: :story_collections

end
class StoryCollection < ActiveRecord::Base

    belongs_to :story
    belongs_to :collection

end

您缺少允许参数
story[collection\u id]

def story_params
  params.require(:story).permit(
    :title,
    :description,
    collection_ids: [], # you need to whitelist this, so the value gets set
    category_ids: [],
    photos_attributes: [
      :id,
      :file_name,
      :file_name_cache,
      :_destroy
    ]
  )
end

完美的谢谢
    <%= f.select :collection_ids, Collection.all.pluck(:name, :id), {}, { multiple: true, class: "selectize" } %>
class CollectionsController < ApplicationController


  def create
    @collection = current_user.collections.build(collection_params)
    if @collection.save
      render json: @collection
    else
      render json: {errors: @collection.errors.full_messages}
    end
  end

  private

    def collection_params
      params.require(:collection).permit(:name, :description)
    end
end
class StoriesController < ApplicationController

  def new
    @story = Story.new
    authorize @story
  end

  def create
    @story = current_user.stories.build(story_params)
    authorize @story
  end

  private

  def story_params
    params.require(:story).permit(:title, :description, category_ids: [],
    photos_attributes: [:id, :file_name, :file_name_cache, :_destroy])
  end
end
  create_table "story_collections", force: :cascade do |t|
    t.integer  "story_id"
    t.integer  "collection_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
def story_params
  params.require(:story).permit(
    :title,
    :description,
    collection_ids: [], # you need to whitelist this, so the value gets set
    category_ids: [],
    photos_attributes: [
      :id,
      :file_name,
      :file_name_cache,
      :_destroy
    ]
  )
end