fields_for 创建新 parent 记录时未创建记录
fields_for not creating a record when making a new parent record
我试图在创建 parent(项目)记录时自动创建 child 记录(参与者)。我可以很好地创建 parent(项目),并且在其他形式上,我可以创建 child(参与者)。我似乎无法在 parent.
的同时创建 child (参与者)
我在 Rails 4,所以我仔细设置了强参数。我只是不明白我做错了什么。
Parent 控制器:
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。
- 当您在应用程序中使用 Rails 4 时,您不需要调用
accepts_nested_attributes_for
,因为您已经在控制器中调用了 params.require
。
- 在
@participant = Participant.new
之后您没有调用 Participant.save
操作。你确实在你的 if
条件中调用了 @project.save
并且你也应该为你的 @participant
这样做。您可以在重定向到 project_path
之前调用 @project.save
。我不确定这是否是一种正确的方法,但是您可以尝试一下是否有效。 :-)
我相信我看到了这个问题。在您的 new_project
操作中,试试这个:
def new_project
@title = params[:ti]
@project = Project.new
@project.participants.build
end
详细说明:如果关联是 blank/empty,fields_for
将不会呈现任何内容。您需要至少有一个由 @project.participants
返回的 participant
才能查看其字段。 @project.participants.build
只会将 Participant
class 的新实例插入到关联中。
我试图在创建 parent(项目)记录时自动创建 child 记录(参与者)。我可以很好地创建 parent(项目),并且在其他形式上,我可以创建 child(参与者)。我似乎无法在 parent.
我在 Rails 4,所以我仔细设置了强参数。我只是不明白我做错了什么。
Parent 控制器:
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。
- 当您在应用程序中使用 Rails 4 时,您不需要调用
accepts_nested_attributes_for
,因为您已经在控制器中调用了params.require
。 - 在
@participant = Participant.new
之后您没有调用Participant.save
操作。你确实在你的if
条件中调用了@project.save
并且你也应该为你的@participant
这样做。您可以在重定向到project_path
之前调用@project.save
。我不确定这是否是一种正确的方法,但是您可以尝试一下是否有效。 :-)
我相信我看到了这个问题。在您的 new_project
操作中,试试这个:
def new_project
@title = params[:ti]
@project = Project.new
@project.participants.build
end
详细说明:如果关联是 blank/empty,fields_for
将不会呈现任何内容。您需要至少有一个由 @project.participants
返回的 participant
才能查看其字段。 @project.participants.build
只会将 Participant
class 的新实例插入到关联中。