【问题标题】:fields_for not creating a record when making a new parent recordfields_for 在创建新父记录时不创建记录
【发布时间】:2015-03-04 14:33:42
【问题描述】:

我试图在创建父(项目)记录时自动创建子记录(参与者)。我可以很好地创建父级(项目),并且在其他表单上,我可以创建子级(参与者)。我似乎无法与父级同时创建子级(参与者)。

我在 Rails 4 上,所以我仔细设置了我的强参数。我只是不明白我做错了什么。

父控制器:

class ProjectsController < ApplicationController

  def new_project
    @title = params[:ti]
    @project = Project.new  
    @project.participants.build
  end


def create_project
  @project = Project.new(project_params)
  @template = Template.find(params[:t]) 
   @project.participants.build
   @title = params[:ti]
  respond_to do |format|
     if @project.save
        @project.participants.save
        format.html { redirect_to new_milestones_path(:p => @project.id), notice: 'Great! We saved your project details.' }
      else
        format.html {   redirect_to  new_project_path(t: @template.id, ti: @title)         
 }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  def project_params
    params.require(:project).permit( :id, :title, :starts, participants_attributes: [:id, :email, :title, :status, :project_id])
  end
end

型号:

 class Participant < ActiveRecord::Base
   belongs_to :project, inverse_of: :participants
   ........
 end

 class Project < ActiveRecord::Base
   has_many :participants, dependent: :destroy, inverse_of: :project
   accepts_nested_attributes_for :participants, allow_destroy: true, reject_if: proc { |a| a["email"].blank? }
   .........
 end

表格:

 <%= form_for @project, url: create_project_path(ti: @title), html: { :multipart => true, :class=> "form-horizontal", id: "basicForm" }do |f| %> 

   <%= f.fields_for :participants do |ff|%>

     <%= ff.hidden_field :email, :value => current_user.email %>
     <%= ff.hidden_field :title, :value => 'Organizer' %>
     <%= ff.hidden_field :status, :value => 'accepted' %>

   <% end %> 
   <%=  f.text_field  :title, :placeholder => 'Your Project Title'%>
   <%=  f.text_field  :starts, :placeholder => 'mm/dd/yyyy'%>

   <%= f.submit ' SAVE PROJECT' %>  
 <% end %>

更新: 我按照 Samo 的建议添加了@project.participants.build(并且我已经更新了上面的代码),这使得 fields_for 可见......但我的项目没有保存......它重定向回 new_project_path。

【问题讨论】:

  • 在做嵌套表单时,我通常还必须将&lt;association&gt;_attributes 添加到attr_accessible 列表中。所以在你的情况下,attr_accessible :participants_attributes。不确定 Rails 4 中是否仍然如此。此外,您的 create 操作中不应该需要 @participant = Participant.new。这没有做任何事情。
  • 谢谢,但 attr_accessible 不再需要了……它已被强参数取代.....stackoverflow.com/questions/17371334/…
  • 您的参与者字段是否显示在表单上?

标签: ruby-on-rails forms ruby-on-rails-4 nested-attributes fields-for


【解决方案1】:

我相信我看到了这个问题。在您的 new_project 操作中,试试这个:

  def new_project
    @title = params[:ti]
    @project = Project.new  
    @project.participants.build
  end

详细说明:如果关联为空白/空,fields_for 将不会呈现任何内容。您需要至少有一个由@project.participants 返回的participant 才能查看其字段。 @project.participants.build 将简单地将 Participant 类的新实例插入到关联中。

【讨论】:

  • 谢谢你,我以后得试试这个……因为我出去了。
  • :participants 字段没有显示出来......你是对的。我添加了@project.participants.build,它们出现了......但它不会创建我的父母和孩子,它会调用“else”重定向并重定向回“new_project”......
  • 如果不保存,则其中一个对象一定是无效的。在保存之前尝试检查@project@project.participants.first 的属性。看看缺少什么。或者使用save! 抛出异常,它应该告诉你哪个验证失败。
  • 是的。我错过了一个必填字段。谢谢!
【解决方案2】:
  1. 当您在应用程序中使用 Rails 4 时,您不需要调用 accepts_nested_attributes_for,因为您已经在 Controller 中调用了 params.require
  2. @participant = Participant.new 之后,您没有调用Participant.saveaction。您确实在if 条件内调用了@project.save,您也应该为您的@participant 这样做。您可以在重定向到project_path 之前致电@project.save。我不确定这是否是正确的方法,但你可以试试它是否有效。 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多