【发布时间】:2014-10-07 18:54:05
【问题描述】:
抱歉,如果这个问题已经得到解答,但我找不到任何可以帮助我的东西。我是 Rails 的新手,所以请温柔:D
我一直在努力让嵌套表单工作,我确信我在去年使用 Rails 3 和 railscasts 演示让嵌套表单工作,但 Rails 4 击败了我。
查看日志,正在运行查询以提取关联表的数据,但表单中没有呈现任何内容。
我已经阅读了许多网站,但到目前为止都没有任何帮助,我不知道从哪里开始。我关注的最新文章是这个http://www.createdbypete.com/articles/working-with-nested-forms-and-a-many-to-many-association-in-rails-4/
视图中仍然没有呈现任何内容。
我从哪里开始调试这个,也许我的 Rails 安装坏了?但我可能遗漏了一些重要的东西。
谢谢, 罗伊斯
编辑 - 我添加了一些控制器和有问题的视图
surveys_controller.rb
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy, :answers]
# GET /surveys
# GET /surveys.json
def index
@surveys = Survey.all
end
# GET /surveys/1
# GET /surveys/1.json
def show
end
# GET /surveys/new
def new
@survey = Survey.new
end
# GET /surveys/1/edit
def edit
end
# POST /surveys
# POST /surveys.json
def create
@survey = Survey.new(survey_params)
respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: @survey }
else
format.html { render :new }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /surveys/1
# PATCH/PUT /surveys/1.json
def update
respond_to do |format|
if @survey.update(survey_params)
format.html { redirect_to @survey, notice: 'Survey was successfully updated.' }
format.json { render :show, status: :ok, location: @survey }
else
format.html { render :edit }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
# DELETE /surveys/1
# DELETE /surveys/1.json
def destroy
@survey.destroy
respond_to do |format|
format.html { redirect_to surveys_url, notice: 'Survey was successfully destroyed.' }
format.json { head :no_content }
end
end
def answers
@participants = Participant.all
@questions = @survey.questions
end
private
# Use callbacks to share common setup or constraints between actions.
def set_survey
@survey = Survey.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def survey_params
params.require(:survey).permit(:name,
:questions_attributes => [:id, :content,
:answers_attributes => [:id, :content, :participant_id]
])
end
end
participents_controller.rb
class ParticipantsController < ApplicationController
before_action :set_participant, only: [:show, :edit, :update, :destroy]
# GET /participants
# GET /participants.json
def index
@participants = Participant.all
end
# GET /participants/1
# GET /participants/1.json
def show
end
# GET /participants/new
def new
@participant = Participant.new
end
# GET /participants/1/edit
def edit
end
# POST /participants
# POST /participants.json
def create
@participant = Participant.new(participant_params)
respond_to do |format|
if @participant.save
format.html { redirect_to @participant, notice: 'Participant was successfully created.' }
format.json { render :show, status: :created, location: @participant }
else
format.html { render :new }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /participants/1
# PATCH/PUT /participants/1.json
def update
respond_to do |format|
if @participant.update(participant_params)
format.html { redirect_to @participant, notice: 'Participant was successfully updated.' }
format.json { render :show, status: :ok, location: @participant }
else
format.html { render :edit }
format.json { render json: @participant.errors, status: :unprocessable_entity }
end
end
end
# DELETE /participants/1
# DELETE /participants/1.json
def destroy
@participant.destroy
respond_to do |format|
format.html { redirect_to participants_url, notice: 'Participant was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participant
@participant = Participant.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participant_params
params.require(:participant).permit(:name)
end
end
application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
answers.html.erb
<h1><%= @survey.name %> Answers</h1>
<%= form_for(@survey) do |f| %>
<% @participants.each do |participant| -%>
<h3><%= participant.name %></h3>
<table>
<thead>
<tr>
<td>Questions</td>
<td>Answer</td>
</tr>
</thead>
<tbody>
<% @questions.each do |question| -%>
<tr>
<td><%= question.content %></td>
<td>
<%= f.fields_for :questions, question do |q| -%>
<%= q.fields_for :answers, question.answers.find_or_initialize_by(participant: participant) do |a| -%>
<%= a.text_area :content %>
<%= a.hidden_field :participant_id, participant.id %>
<% end -%>
<% end -%>
</td>
</tr>
<% end -%>
</tbody>
</table>
<% end -%>
<div class="actions">
<%= f.submit %>
</div>
<% end -%>
【问题讨论】:
-
快速建议:在这里发布一些代码。也许是控制器方法和视图本身。
-
干杯迈尔斯,希望对您有所帮助。
-
请发布调查模型的内容。你可能错过了
accept_nested_attributes_for :questions -
嵌套表单丑陋、混乱和脆弱——你需要记住在整个堆栈中正确地做很多小事情。通过使用表单对象,您可以使事情变得更容易理解并帮助隔离更改。表单对象伪装成模型,因此在处理控制器或视图时它们的工作方式与模型相同。有一个关于它的专业 Railscast,或参见:stackoverflow.com/questions/25296385/…
-
这看起来很有趣,将不得不阅读它
标签: ruby-on-rails nested-forms