我在rails 4中进行编程。我已经完成了一个嵌套的表单,通过JavaScript添加了attribuets,接下来 railscasts
我有feast
。它有很多dishes
到courses
和users
到participations
。
每个用户获得feast
这样的分配:
参与有很多dishes
到obligations
。
我的问题是:为什么不能保存?节日保存,但嵌套的形式部分不... 基本上我认为这是一个很强的params问题,或者它应该在控制器或模型中。所以我先粘贴它们。我粘贴了程序生成的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
和参与:
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
程序很复杂,所以最后我将发布为一个参与者创建的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%>
我的控制器feasts_controller:
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
这是生成的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>
为什么嵌套部分不能保存?
答案 0 :(得分:0)
我不知道这个问题,但我会采取一些措施来调试和缩小您的问题:
raise feast_params.inspect
。那符合你的期望吗?rails console
会话中,尝试创建嵌套模型(Feast.new(participations_attributes: {...})
)。 答案 1 :(得分:0)
所以这里是: 我尝试了2个插件,他们没有工作,所以我认为我做错了 一般形式。
我使用匹配的路线。当我改为REST路线时,一切正常。
探测到之前的表格去除线:
<%= form_for(:feast, :url => {:action => 'create'}) do |f| %>
缺少@feast变量,所以它可以将嵌套部分附加到它。 所以也许有办法解决它,即使匹配的路线可能是这样的:
<%= form_for(:feast, @feast, :url => {:action => 'create'}) do |f| %>
但是从那以后 我改为REST路由它的工作,我不用费心去检查。