Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 Rails 4嵌套属性和sti模型更新属性创建新记录_Ruby On Rails_Nested Attributes_Sti - Fatal编程技术网

Ruby on rails Rails 4嵌套属性和sti模型更新属性创建新记录

Ruby on rails Rails 4嵌套属性和sti模型更新属性创建新记录,ruby-on-rails,nested-attributes,sti,Ruby On Rails,Nested Attributes,Sti,我有一个rails 4实例,它运行的页面模型具有不同类型的页面内容,比如从PageContent继承的PageContent::Text 在我的表单中,我呈现了您可以在表单片段中看到的所有@page.page\内容 如果我更新我的页面,记录不会得到更新,它们将被创建为新记录 # update controller class Admin::PagesController < Admin::BaseController def update @page = Page.find_

我有一个rails 4实例,它运行的页面模型具有不同类型的页面内容,比如从PageContent继承的PageContent::Text

在我的表单中,我呈现了您可以在表单片段中看到的所有@page.page\内容

如果我更新我的页面,记录不会得到更新,它们将被创建为新记录

# update controller
class Admin::PagesController < Admin::BaseController

  def update
    @page = Page.find_by_url(params[:id])

    respond_to do |format|
      if @page.update_attributes(page_params(@page.type.parameterize.gsub('page','')))
        format.html { redirect_to edit_admin_page_path(@page) }
      else
        format.html { render :action => "edit" }
      end
    end
  end

  def page_params(type)
    params.require("#{type}_page".to_sym).permit(:name, :contents_attributes => [:body, :type])
  end

end

# content model
class PageContent::Text < PageContent
end

# page model
class Page < ActiveRecord::Base
  has_many :contents, class_name: 'PageContent', dependent: :destroy
  accepts_nested_attributes_for :contents
end

# form snippet
<textarea class="form-control wysihtml5" id="content_page_contents_attributes_0_body" name="content_page[contents_attributes][0][body]">
testsetseet</textarea>
<input id="content_page_contents_attributes_0_type" name="content_page[contents_attributes][0][type]" type="hidden" value="PageContent::Text" />
<input id="content_page_contents_attributes_0_id" name="content_page[contents_attributes][0][id]" type="hidden" value="1" />
#更新控制器
类Admin::PagesController“edit”}
结束
结束
结束
def页面参数(类型)
参数.require(“#{type}\u page.”to_sym).permit(:name,:contents_attributes=>[:body,:type])
结束
结束
#内容模型
类PageContent::Text
任何想法都是非常受欢迎的


提前感谢

也许只需在
中添加
:id
。许可证(:name,:contents\u attributes=>[:body,:type])
就能解决问题吗

结果应该类似于
:contents\u attributes=>[:body,:type,:id]


还可以查看服务器stdout-它输出哪些属性是不允许的。

可能只是将
:id
添加到
。允许(:name,:contents\u attributes=>[:body,:type])
可以解决问题吗

结果应该类似于
:contents\u attributes=>[:body,:type,:id]


还可以查看服务器标准输出-它输出哪些属性是不允许的。

可能只是将
:id
添加到
。允许(:name,:contents\u attributes=>[:body,:type])
可以解决问题吗?比如
:contents\u attributes=>[:body,:type,:id]
。还可以查看服务器stdout-它输出哪些属性是不允许的。向允许的属性添加id解决了问题!!太棒了,谢谢!我把它作为答案贴出来,这样你就可以接受了。谢谢。也许只需将
:id
添加到
。permit(:name,:contents\u attributes=>[:body,:type])
就能解决问题吗?比如
:contents\u attributes=>[:body,:type,:id]
。还可以查看服务器stdout-它输出哪些属性是不允许的。向允许的属性添加id解决了问题!!太棒了,谢谢!我把它作为答案贴出来,这样你就可以接受了。谢谢。这个答案帮助我解决了一个我已经研究了好几天的问题!非常感谢。但我有一个问题-允许用户更改id是否安全?不幸的是,
:id
未标记为不允许。它只是创建一个新记录,直到您将
:id
添加到允许的属性中,并且嵌套的记录最终可以更新。非常感谢。这个答案帮助我解决了一个我已经研究了好几天的问题!非常感谢。但我有一个问题-允许用户更改id是否安全?不幸的是,
:id
未标记为不允许。它只是创建一个新记录,直到您将
:id
添加到允许的属性中,并且嵌套的记录最终可以更新。非常感谢。