Ruby on rails 4 轨道嵌套形式零件不';即使允许,也不能保存
我正在用rails 4编程。我已经完成了一个嵌套表单,它通过JavaScript添加了属性列表,如下所示 我有一个Ruby on rails 4 轨道嵌套形式零件不';即使允许,也不能保存,ruby-on-rails-4,nested-forms,Ruby On Rails 4,Nested Forms,我正在用rails 4编程。我已经完成了一个嵌套表单,它通过JavaScript添加了属性列表,如下所示 我有一个宴会。通过课程和用户通过参与,它有许多菜肴。 每个用户都会得到类似这样的任务: 参与有许多菜品到义务 我的问题是:为什么它不保存?宴会将保存,但嵌套的表单部分不会。。。 基本上,我想这是一个强参数的问题,或者它应该在控制器或模型中。所以我先把它们贴上。我粘贴了程序生成的HTML,只在最后粘贴了相关部分,这样问题就不会太长了。 谢谢 代码如下: Class Feast < Ac
宴会
。通过课程
和用户
通过参与
,它有许多菜肴
。
每个用户都会得到类似这样的任务:
参与有许多菜品
到义务
我的问题是:为什么它不保存?宴会将保存,但嵌套的表单部分不会。。。
基本上,我想这是一个强参数的问题,或者它应该在控制器或模型中。所以我先把它们贴上。我粘贴了程序生成的HTML,只在最后粘贴了相关部分,这样问题就不会太长了。
谢谢
代码如下:
Class Feast < ActiveRecord::Base
mount_uploader :image, ImageUploader
has_many :participations, dependent: :destroy
has_many :users, :through => :participations
has_many :courses, dependent: :destroy
has_many :dishes, :through => :courses
accepts_nested_attributes_for :participations,
:allow_destroy => true
accepts_nested_attributes_for :courses,
:allow_destroy => true
类宴会:参与
有很多:课程,依赖::破坏
有很多:菜,:到=>:菜
接受以下内容的\u嵌套\u属性\u:participations,
:allow_destroy=>true
接受:课程的\u嵌套\u属性\u,
:allow_destroy=>true
和参与:
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :feast
has_many :obligations, dependent: :destroy
has_many :dishes, :through=> :obligations
has_many :groceries, :as => :needed
accepts_nested_attributes_for :obligations,
:allow_destroy => true
end
课堂参与:义务
有很多:杂货,:as=>:需要
接受:义务的\u嵌套\u属性\u,
:allow_destroy=>true
终止
该计划是复杂的,因此在这个问题的最后我将发布为一名参与者、一道菜和一项作业(=义务)创建的HTML,这样就可以看到该计划运行良好
这是我的嵌套表单——它们的值由js更新和插入。
课程字段部分:
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:courses).klass.new%>
<%= f.fields_for(:courses, new_object, child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:participations).klass.new%>
<%= f.fields_for(:participations, new_object , child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%new_object = Participations.reflect_on_association(:obligations).klass.new%>
<%= fp.fields_for(:obligations, new_object , child_index: "new_obli", class:
"fields") do |fpo| %>
<%= fpo.hidden_field(:dish_id) %>
<%= fpo.hidden_field(:_destroy,value: false) %>
<%end%>
“两者都要”%>
部分参与领域:
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:courses).klass.new%>
<%= f.fields_for(:courses, new_object, child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:participations).klass.new%>
<%= f.fields_for(:participations, new_object , child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%new_object = Participations.reflect_on_association(:obligations).klass.new%>
<%= fp.fields_for(:obligations, new_object , child_index: "new_obli", class:
"fields") do |fpo| %>
<%= fpo.hidden_field(:dish_id) %>
<%= fpo.hidden_field(:_destroy,value: false) %>
<%end%>
“两者都要”%>
义务字段部分:
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:courses).klass.new%>
<%= f.fields_for(:courses, new_object, child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%= link_to_function "remove","remove_fields_dish(this)", :class => "cl_both" %>
<%new_object = Feast.reflect_on_association(:participations).klass.new%>
<%= f.fields_for(:participations, new_object , child_index: "new_course", class: "fields") do
|fc| %>
<%=fc.hidden_field(:dish_id)%>
<%=fc.hidden_field(:_destroy,value: false)%>
<%end%>
<%new_object = Participations.reflect_on_association(:obligations).klass.new%>
<%= fp.fields_for(:obligations, new_object , child_index: "new_obli", class:
"fields") do |fpo| %>
<%= fpo.hidden_field(:dish_id) %>
<%= fpo.hidden_field(:_destroy,value: false) %>
<%end%>
我的管制员宴请您的管制员:
class FeastsController < ApplicationController
def list
@user_feast_m=Feast.joins(participations: :user).where(participations: {manager:
true}).to_a
@user_feast_p=Feast.joins(participations: :user).where(participations: {manager:
false}).to_a
end
def show
@feast=feast.find(params[:id])
@users=@feast.users
@dishes=(Dish.joins(obligations: {participation: :feast}).where(feast:
{id:@feast.id }).to_a + @feast.dishes).compact
end
def new
@feast= Feast.new
@myself = User.find(session[:user_id])
respond_to do |format|
format.html
format.js
end
end
def create
@feast = Feast.new(feast_params)
if @feast.save
flash[:notice]="the feast has been saved. all participants will get invitations and
assignments. hope they answer soon"
redirect_to(:action=>'list')
else
render('new')
end
end
def update
end
def edit
@feast=feast.find(params[:id])
@users=@feast.users.sort{|user1,user2| user2.manager <=>
user1.manager}.sort_by{|user| user.name}
@dishes=(Dish.joins(obligations: {participation: :feast}).where(feast:
{id:@feast.id }).to_a + @feast.dishes).compact
end
def delete
@feast=Feast.find(params[:id])
end
def destroy
end
private
def feast_params
params.require(:feast).permit(:id, :name, :image, :feast_place,
:feast_time,courses_attributes: [:id,
:dish_id,:_destroy,:feast_id],participations_attributes: [:id,:feast_id,
:user_id, :_destroy],obligations_attributes: [:id, :dish_id, :_destroy,
:participation_id])
end
end
class FeastsController'list')
其他的
呈现('新')
终止
终止
def更新
终止
定义编辑
@feast=feast.find(参数[:id])
@users=@feast.users.sort{| user1,user2 | user2.manager
user1.manager}.sort_by{| user | user.name}
@菜肴=(Dish.join(义务:{参与::宴会})。其中(宴会:
{id:@feast.id}).to_a+@feast.diseases.compact
终止
def删除
@feast=feast.find(参数[:id])
终止
def销毁
终止
私有的
def feast_params
参数require(:feast).permit(:id,:name,:image,:feast\u place,
:宴会时间,课程属性:[:id,
:dish\u id、:\u destroy、:feast\u id]、参与度属性:[:id、:feast\u id、,
:user\u id,:\u destroy],义务属性:[:id,:dish\u id,:\u destroy,
:参与(身份证)
终止
终止
这是生成的HTML:
<form accept-charset="UTF-8" action="/feasts/create" enctype="multipart/form-data"
method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8"
type="hidden" value="✓"><input name="authenticity_token" type="hidden"
value="llJ73mQ6yIZHKvs1EDHnVbjc1nIGlWN2eCirXwiCgCs="></div>
#+> some input fields for the feast goes here
<br><br>
<label for="feast_feast place">feast_place</label><br><br>
<input id="feast_feast_place" name="feast[feast_place]" type="text"> <br><br>
upload image file <br><br>
<input id="feast_image" name="feast[image]" type="file"> <br><br>
<h2>participants
<u>
<a class="right" data-content="<br />
<a class="cl_both" href="#"
onclick="remove_fields(this); return false;">remove</a>
<input id="feast_participations_user_id" name=";feast[participations][user_id]"
type="hidden" />
<input id="feast_participations__destroy" name="feast[participations][_destroy]"
type="hidden" value="false" />
<div data-obcontent='
<input id="feast_participations_obligations_attributes_new_obli_dish_id"
name="feast[participations][obligations_attributes][new_obli][dish_id]"
type="hidden"/>
<input id="feast_participations_obligations_attributes_new_obli__destroy"
name="feast[participations][obligations_attributes][new_obli][_destroy]"
type="hidden" value="false" />
' >
</div>
" href="#" id="try" onclick="add_par(); return false;">
<img alt="Photo Gallery" height="100" src="/assets/add_participants.png"
width="100">
</a> </u>
</h2>
<br><br><br>
<div data-myid="3" data-myimage=""image":{"url:"/uploads/user/image/3/el4.jp"}}" data-
myname="elad bezalel" id="par">
<table>
<tbody><tr>
<th>participant</th>
<th>assignment</th>
</tr>
<tr><td><div class="user_block ui-droppable" style=""><a href="/users/show?
id=7" id="1404733550784"><img alt="user picture"
src="/uploads/user/image/7/Yemenite.gif" height="100" width="100">
<br>amit</a><br>
<a class="cl_both" href="#" onclick="remove_fields(this); return
false;">remove</a>
<input id="1404733550977" name="feast[participations][user_id]"
type="hidden" value="7">
<input id="1404733554049" name="feast[participations]
[obligations_attributes][1404733554050][dish_id]" type="hidden"
value="140473354199">
<input id= "feast_participations_obligations_attributes_1404733554050__destroy"
name="feast[participations][obligations_attributes][1404733554050][_destroy]"
type="hidden" value="false">
<input id="feast_participations__destroy" name="feast[participations]
[_destroy]" type="hidden" value="false">
<div data-obcontent="
<input id="feast_participations_obligations_attributes_new_obli_dish_id"
name="feast[participations][obligations_attributes][new_obli][dish_id]"
type="hidden" ></div>
<input id="feast_participations_obligations_attributes_new_obli__destroy"
name="feast[participations][obligations_attributes][new_obli][_destroy]"
type="hidden" value="false" />
">
</div>
</div></td><td><dfn>e </dfn><img
alt="cancel - x" class="x-pic" height="30" src="/assets/x-blue.png" width="30">
</td></tr></tbody></table>
</div>
<br><br><br><br><br><br>
<h2>dishes
<u>
<a class="right" data-content="<br />
<a class="cl_both" href='#' onclick='remove_fields_dish(this); return
false'>remove</a>
<input id="feast_courses_dish_id" name="feast[courses][dish_id]" type="hidden" />
<input id="feast_courses__destroy" name="feast[courses][_destroy]"type="hidden"
value="false" />
" href="#" id="add_course" onclick="add_course(); return false;">
<img alt="Photo Gallery" height="100" src="/assets/add_dish.png" width="100">
</a> </u>
</h2>
<br><br><br>
<div id="course">
<table>
<tbody><tr><td><div class="dish_block ui-draggable ui-draggable-handle"
style="position: relative; top: 0px; left: 0px;"><a href="/dishes/show?id=9"
id="1404733541991"><img alt="dish picture"
src="/uploads/dish/image/9/IMG_0012.JPG" height="100" width="100"><br>e</a><br>
<a class="cl_both" href="#" onclick="remove_fields_dish(this); return
false;">remove</a>
<input id="1404733542182" name="feast[courses][dish_id]" type="hidden" value="9">
<input id="feast_courses__destroy" name="feast[courses][_destroy]" type="hidden"
value="false">
</div></td></tr></tbody></table>
</div>
<br><br>
<input name="commit" type="submit" value="initiate feast">
</form>
#+>这里有一些宴会的输入字段
宴会地点
上载图像文件
参与者
“href=”id=“try”onclick=“add#u par();返回false;">
为什么嵌套部分不保存?我不知道问题出在哪里,但我会做一些事情来调试并缩小问题范围:
- 在控制器中,
。这符合您的期望吗提高宴会参数。检查
- 在
会话中,尝试创建嵌套模型(rails控制台
)Feast.new(participations\u attributes:{…})
<%= form_for(:feast, :url => {:action => 'create'}) do |f| %>
{:action=>'create'})do | f |%>
缺少@feast变量,因此它可以将嵌套部分附加到该变量中。
因此,也许有一种方法可以修复它,即使是匹配的路线,可能类似于:
<%= form_for(:feast, @feast, :url => {:action => 'create'}) do |f| %>
{:action=>'create'})do | f |%>
但是自从
我改为REST路由它工作,我不必检查。问题是您没有与家长一起创建您的孩子记录。在控制器的
新方法中,您应该有@feast.participations.build
和@feast.courses.build
。在rails中,rayn通过链接创建新的孩子记录。所以他写了,所以我现在试着在我的部分中这样做,但仍然不起作用……有什么想法吗?