ruby-on-rails - 为什么更新操作不适用于 rails 中的嵌套表单字段?

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

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

编辑 Controller :

def edit
    @profile = current_user.profile
end

更新 Controller :
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

问题是,更新 Controller 正在为非嵌套信息工作,它只是不适用于嵌套作业。以下是强参数:
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

提前感谢您的帮助。

编辑

以下是来自 create 操作的参数:
{"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

最佳答案

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

关于ruby-on-rails - 为什么更新操作不适用于 rails 中的嵌套表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774808/

相关文章:

ruby - Rails 如果 @model.save 抛出错误

从索引链接加载页面时,Javascript 未加载

ruby-on-rails - ActiveRecord错误: Couldn't find User without an ID

ruby-on-rails - 嵌套包含与 Rails 上的 where

ruby-on-rails - Rails 使用参数测试 Controller 私有(private)方法

ruby-on-rails - 如何在rails Devise gem中为单个用户关联多个电子邮件

ruby-on-rails - RSpec:主页的集成测试

ruby-on-rails - 如何显示 'recently visited'?

ruby-on-rails - 使用 Rails 和 PostgreSQL 的双主键索引

ruby-on-rails - 如何在 Capybara/Rspec 中 stub 回形针文件路径