【问题标题】:Rails 4 Strong Parameters - Stack level too deep on updateRails 4强参数-更新时堆栈级别太深
【发布时间】:2013-01-26 19:36:01
【问题描述】:

我一直在使用 Rails 4,但在更新具有强参数的记录时遇到了一些问题。我不断收到“堆栈级别太深”错误。我正在尝试更新 has_one meta_data 的帖子记录。

post.rb

  has_one :meta_data, :as => :meta_dataeable, :dependent => :destroy
  accepts_nested_attributes_for :meta_data
  after_initialize do
    self.build_meta_data unless self.meta_data.present?
  end

posts_controller.rb

def create
  @post = Post.create(permitted_params)
  redirect_to :action => 'index'
end

def update
  @post = Post.find(params[:id])
  @post.update_attributes(permitted_params)
  redirect_to :action => 'index'
end

def permitted_params
  params.require(:post).permit(
    :title, 
    :body, 
    :excerpt, 
    :permalink, 
    :content_type, 
    :author_id, 
    :media, 
    :commenting, 
    :published_at, 
    :public, 
    {:meta_data_attributes => [:title, :description, :keywords, :menu_name]}
  )    
end

创建新记录没有任何问题,并保存关联的 meta_data 记录。更新给了我一个 Stack Level Too Deep 错误。当我从允许的参数中删除 {:meta_data_attributes => [:title, :description, :keywords, :menu_name]} 时,保存工作没有问题

任何帮助都会很棒,在此先感谢您!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 strong-parameters


    【解决方案1】:

    找出问题所在,需要添加:id作为meta_data_attributes的允许参数。

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 1970-01-01
      • 2017-08-19
      • 2018-09-12
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多