【问题标题】:Why won't update action work for nested form fields in rails?为什么不更新 Rails 中嵌套表单字段的操作工作?
【发布时间】:2013-11-04 18:45:59
【问题描述】:

我有一个属于_个人资料模型的工作模型。个人资料有很多工作。我有一个嵌套模型表单,用户可以在其中添加作业。表单已成功添加作业,但无法编辑/更新。相反,当我尝试编辑作业时,它会保留旧版本的作业,但也会添加新版本。它不会用新版本替换旧版本。我该如何解决?我很确定这与编辑/更新控制器有关。

编辑控制器:

def edit
    @profile = current_user.profile
end

更新控制器:

def update
    #if current_user.profile.jobs.any?

    @profile = current_user.profile.update_attributes(profile_params)

    if current_user.profile.invalid?
        @profile = current_user.profile
        render :edit, :status => :unprocessable_entity
    else
        redirect_to profile_path(current_user.profile_name)
    end
end

问题是,更新控制器适用于非嵌套信息,它只是不适用于嵌套作业。以下是强参数:

def profile_params
      params.require(:profile).permit(:title,
           :category, :description, :state, :zip_code, :rate, 
           jobs_attributes: [:firm, :position, :category, :description,
           :begin, :end, :_destroy])        
end

这是配置文件模型:

class Profile < ActiveRecord::Base
    belongs_to :user
    has_many :jobs
    accepts_nested_attributes_for :jobs , :reject_if => :all_blank, :allow_destroy => true
end

另外,如果有帮助,这是我的路线:

resources :profiles do
   resources :jobs
end

提前感谢您的帮助。

编辑

以下是创建操作的参数:

{"jobs_attributes"=>{"1383593195849"=>{"firm"=>"1st firm", "position"=>"1st position",
 "category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
 "end"=>"1999", "_destroy"=>"false"}}}

以下是更新时同一作业的参数:

{"jobs_attributes"=>{"0"=>{"firm"=>"1st firm", "position"=>"1st position",
 "category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
 "end"=>"1999", "_destroy"=>"false"}, "1"=>{"firm"=>"1st firm", 
 "position"=>"1st position", "category"=>"1st category", 
 "description"=>"1st description", "begin"=>"1999", "end"=>"1999", "_destroy"=>"1"}}}

编辑:

这是我的看法。我不认为他们是问题的一部分。

= simple_form_for @profile do |f|

  %h3 Jobs
  #jobs
    = f.simple_fields_for :jobs do |job|
      = render 'job_fields', :f => job
    .links
      = link_to_add_association 'add job', f, :jobs
  = f.submit

这里是“job_fields”部分:

.nested-fields
  = f.input :firm
  = f.input :position
  = f.input :category
  = f.input :begin
  = f.input :end
  = f.input :description
  = f.input :_destroy, as: :boolean, inline_label: 'Delete box'
  = link_to_remove_association "remove task", f

【问题讨论】:

  • 发布更新动作的参数
  • 参数中有两个作业需要更新。这就是它的本意吗?你能发表你的看法吗?
  • link665,不,它不应该是那样的。这就是问题所在,而不是更新对象,而是创建一个新实例。我现在将发布意见。
  • simple_fields_for 方法是什么?
  • @link664 来自 Simple Forms gem。谷歌会很好的。

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord nested-forms nested-attributes


【解决方案1】:

诀窍是将':id' 符号添加到强参数中。虽然我还没有弄清楚为什么,我不确定它是否安全。

【讨论】:

  • 在嵌套表单 gem 的嵌套属性方面遇到了同样的问题。对于更新和删除,您确实必须添加 :id 和可选的 :_destroy github.com/ryanb/nested_form#strong-parameters
  • 是的,它是安全的,这是nested_attributes 起作用的唯一方法。父控制器的更新动作需要知道嵌套子节点的 id,否则它无法找到它们来更新它们。如果您不提供 id(或者不在控制器许可方法中将它们列入白名单),那么更新将创建新对象,如您所见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多