undefined method `with_indifferent_access' for…” when passing it nested attributes
というエラーが出て、うーんRails4になって久しぶりにいじるとなにがなんだかでしたが、解決したため記録しておきます。
mongoidを使ってますが、使い方はどっちも変わらないです。笑
class Article
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :sub_title, type: String
has_many :prices
accepts_nested_attributes_for :prices #追加
end
class Priceinclude Mongoid::Documentfield :article_id, type: Stringfield :type, type: Integerbelongs_to :articleendclass ArticleController < ActionController::Base def create@article = Article.new(article_params) #pricesを含めて入れてくれる@prices = @article.prices.build(article_params["prices"]) #不要@prices.save #不要respond_to do |format|if @article.saveformat.html { redirect_to @article, notice: 'Article was successfully created.' }format.json { render action: 'show', status: :created, location: @article }elseformat.html { render action: 'new' }format.json { render json: @article.errors, status: :unprocessable_entity }endendend privatedef article_paramsparams.require(:article).permit(:title, :sub_title, prices_attributes: [:type]) #attribute名に注意endend
Rails 4 permit nested attributes doesn't create/update || stackoverflow
“NoMethodError (undefined method `with_indifferent_access' for…” when passing it nested attributes || stackoverflow
Rails 4 Nested Attributes Unpermitted Parameters || stackoverflow
Strong Parameters || Rails公式サイト