레일 4에서 프로그래밍 중입니다. railscasts 다음에 JavaScript에 의해 추가 된 속성을 중첩시킨 양식을 작성했습니다.레일 중첩 된 폼 파트가 허용되는대로 저장되지 않습니다.
나는 feast
입니다. dishes
~ courses
및 users
~ participations
이 있습니다. 각 사용자는 feast
과 같은 과제를 얻습니다. 참여 횟수는 dishes
부터 obligations
까지 많습니다.
내 질문은 : 왜 저장하지 않습니까? 절기는 저장하지만 중첩 된 폼 파트는 그렇지 않습니다. 기본적으로 강한 패러미터에 문제가있는 것 같거나 컨트롤러 또는 모델에 있어야합니다. 그래서 나는 그들을 먼저 붙였다. 나는 프로그램에 의해 생성 된 HTML을 붙여 넣었습니다. 결국 관련 부분 만이 문제를 너무 길게 만들지 않았습니다. 나는이 질문의 끝에 내가 한 참가자에 대해 생성 된 HTML을 게시 할 예정 있도록 프로그램이 복잡
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
:
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
과 참여 : 감사
이 코드입니다 1 접시 및 1 개의 지정 (= 의무) 다만 프로그램이 잘 작동하는 본다.
여기에 내 중첩 된 양식 - 그 값은 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])
@[email protected]
@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])
@[email protected]{|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="https://stackoverflow.com/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>
왜 중첩 된 부분이 저장되지 않습니까?
문제는 부모 레코드와 함께 자식 레코드를 빌드하지 않는다는 것입니다. 컨트롤러의'new' 메소드에서'@ feast.participations.build' 및'@ feast.courses.build'가 있어야합니다. 레일 캐스팅에서 – Pavan
rayn은 링크를 통해 새 하위 레코드를 작성합니다. (코스), new_object, child_index : "new_course", 클래스 : "필드") do | fc | %> 그래서 지금은 부분적으로 해보려고했으나 여전히 효과가 없습니다. 어떤 아이디어입니까? – user3294287