Rails 4.0.3에서 중첩 된 리소스로 중첩 된 form_for을 만들려고합니다. 그러나 몇 가지 문제가있어서 많은 시간을 보냈습니다.중첩 된 리소스 레일이있는 중첩 된 폼 4
문제점은 양식 제출을 시도 할 때 값이 내 데이터베이스에 저장되지 않을 때입니다. labs#index
다음과 같이 중첩 리소스 및 labs_controller
을 정의했습니다.
route.rb
...
resources :users do
resources :labs
end
(부분 그것을) labs_controller.rb
class LabsController < ApplicationController
load_and_authorize_resource
def new
@user = User.find(params[:user_id])
@lab = Lab.new
end
def create
@lab = Lab.new(lab_params)
if @lab.save
flash[:notice] = "Successfully created lab."
redirect_to user_labs_url
else
render :action => 'index'
end
end
private
def lab_params
params.require(:lab).permit(:title, :description,
stages_attributes: [:id, :lab_id, :description,
tasks_attributes: [:id, :stage_id, :detail, :instruction, :answer ]])
end
end
I 3 개 모델 Lab
, Stage
Task
및 및 각 has_many
통해 연결된 한 관계. 나는 또한 CanCan
을 사용하여 인증을 처리합니다.
lab.rb
class Task < ActiveRecord::Base
belongs_to :stage
resourcify
end
class Stage < ActiveRecord::Base
belongs_to :lab
has_many :tasks
resourcify
accepts_nested_attributes_for :tasks, allow_destroy: true
end
task.rb
class Lab < ActiveRecord::Base
belongs_to :user
has_many :stages
resourcify
validates :user_id, presence: true
accepts_nested_attributes_for :stages
end
stage.rb 여기 내 폼 파셜을합니다.
_form.html.erb
<%= form_for [@user, @lab] do |f| %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.label :description%>
<%= f.text_area :description %>
</div>
<%= f.fields_for :stages do |builder| %>
<%= render "labs/partials/stage_fields", f: builder %>
<% end %>
<div><%= f.submit "Submit", class: "btn btn-primary" %></div>
<% end %>
_stage_fields.html.erb
<fieldset>
<%= f.label :description %>
<%= f.text_area :description %>
</fieldset>
<%= f.fields_for :tasks do |builder| %>
<%= render "labs/partials/task_fields", f:builder %>
<% end %>
_task_fields.html.erb
<fieldset>
<%= f.label :detail %>
<%= f.text_area :detail %>
<%= f.label :instruction %>
<%= f.text_area :instruction %>
<%= f.label :answer %>
<%= f.text_field :answer %>
</fieldset>
몇 가지 질문을했고 그 중 아무 것도 나를 위해 일하고 있지 않습니다. 예 : form_for with nested resources
나는 또한 중첩 된 모델 형태 여전히 행운에 라이언 베이츠를 따라하려고
- . 누군가 나를 도울 수 있기를 바랍니다.
미리 감사드립니다.
f가 아닌 partials에서 빌더에 액세스 해보십시오. 또는 partial을 사용하지 않고 그것을 시도해 보자. 하나의 form으로 중첩 된, 아마도 partial이 문제를 야기하고있다. – flylib
@flylib 안녕하세요, 부분적으로 렌더링하지 못하도록 수정했습니다. 그것은 내가 간과 한 부분적인 경로입니다. 하지만 여전히 데이터베이스에 저장되지 않는 이유를 파악할 수 없습니다. –
'form'을 제출할 때'log information'을 게시 할 수 있습니까? – Pavan