Ruby on rails 渲染嵌套部分模板-在两个单独的模型上渲染嵌套表单
我在本期的第二周,最近使用了railsCast。我知道这是老的-也许这是我的问题。另外,我将在Cloud9上托管rails服务器 我试着遵循一些不同的教程,只是为了让其中一个继续&这是我所能做到的。奇怪的是,他们的syntex没有一个与官方RubyonRails文档中的内容相匹配。。。Rails视图模板 在railsCast中,这家伙可以得到空白字段来显示。。。我不知道如何…所以我还没有设法填充问题或答案字段。我甚至不知道这两条rails控制台消息的含义是什么——此外,这里没有记录 感谢您的阅读和任何建议 -M 不用麻烦了,我的先生。。。如railsCast 196所示,通过模板嵌套表单 我的rails控制台Ruby on rails 渲染嵌套部分模板-在两个单独的模型上渲染嵌套表单,ruby-on-rails,templates,nested-forms,partials,Ruby On Rails,Templates,Nested Forms,Partials,我在本期的第二周,最近使用了railsCast。我知道这是老的-也许这是我的问题。另外,我将在Cloud9上托管rails服务器 我试着遵循一些不同的教程,只是为了让其中一个继续&这是我所能做到的。奇怪的是,他们的syntex没有一个与官方RubyonRails文档中的内容相匹配。。。Rails视图模板 在railsCast中,这家伙可以得到空白字段来显示。。。我不知道如何…所以我还没有设法填充问题或答案字段。我甚至不知道这两条rails控制台消息的含义是什么——此外,这里没有记录 感谢您的阅读
2.2.1 :045 > cc = Survey.first.questions.first
Survey Load (0.5ms) SELECT "surveys".* FROM "surveys" ORDER BY "surveys"."id" ASC LIMIT 1
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? ORDER BY "questions"."id" ASC LIMIT 1 [["survey_id", 1]]
=> nil
2.2.1 :046 > cc = Survey.first.questions
Survey Load (0.3ms) SELECT "surveys".* FROM "surveys" ORDER BY "surveys"."id" ASC LIMIT 1
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>
所以我的代码
测量控制器.rb
class SurveysController < ApplicationController
def index
@surveys = Survey.all
end
def show
@survey = Survey.find(params[:id])
end
def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end
def create
@survey = Survey.new(survey_params)
if @survey.save
flash[:notice] = "Successfully created survey."
redirect_to @survey
else
render :action => 'new'
end
end
def edit
@survey = Survey.find(params[:id])
end
def update
@survey = Survey.find(params[:id])
if @survey.update_attributes(params[:survey])
flash[:notice] = "Successfully updated survey."
redirect_to @survey
else
render :action => 'edit'
end
end
def destroy
@survey = Survey.find(params[:id])
@survey.destroy
flash[:notice] = "Successfully destroyed survey."
redirect_to surveys_url
end
private
def survey_params
params.required(:survey).permit(:id, :survey, :notice)
end
end
class SurveysController“新建”
结束
结束
定义编辑
@survey=survey.find(参数[:id])
结束
def更新
@survey=survey.find(参数[:id])
如果@survey.update_属性(参数[:survey])
flash[:notice]=“已成功更新调查。”
将_重定向到@survey
其他的
呈现:操作=>“编辑”
结束
结束
def销毁
@survey=survey.find(参数[:id])
@摧毁
flash[:notice]=“已成功销毁调查。”
将\u重定向到调查\u url
结束
私有的
def测量参数
所需参数(:调查)。许可证(:id,:调查,:通知)
结束
结束
编辑动作视图
<% title = "Edit Survey" %>
<%= render 'form' %>
<p>
<%= link_to "Show", @survey %> |
<%= link_to "View All", surveys_path %>
</p>
|
_form.html.erb
<%= form_for(@survey) do |f| %>
<% if @survey.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>
<ul>
<% @survey.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<% f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
禁止保存此调查:
建筑商%>
_问题_fields.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
<% f.fields_for :answers do |builder| %>
<%= render 'answer_fields', :f => builder %>
<% end %>
<p>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
</p>
3%>
建筑商%>
_答案_fields.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Question" %>
</p>
<% f.fields_for :answers do |builder| %>
<%= render 'answer_fields', :f => builder %>
<% end %>
<p>
<%= f.label :content, "Answer" %>
<%= f.text_field :content %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove" %>
</p>
在我从网络上运行的7个项目中的每一个中
问题出在params.require()中的数组嵌套。。告诉某人它必须嵌套是一回事,当它们是新的时向他们显示语法是另一回事:)
例如:
// Note this is from memory, as I deleted this version of the github..so it's not exactly right or tested...
params.require(:survey).permit(:id,:questions => [:id, :survey_id, :question, :answers => [:id, :question_id, :answer]])
下面是对同一个示例的深入分析:
params.require(:survey).permit(
:id,
:questions => [:id, // This is the 1st nesting
:survey_id, :question, :answers => [:id, // This is 1st nested array ":questions"
:question_id, :answer] // End the 2nd nested array ":answers"
] // End the 2nd array ":questions"
) // End the ":surveys" array & the .permit as a whole
这不是一个答案,这是一个建议。对嵌套表单使用“cocoon”gem。它很容易实现,并且有一个很好的文档。我将附带它的另一个实例和fork github,但是我真的很想对rails的故障排除有足够的了解,这样我就可以了解“魔力”关于rails我自己-我不想成为一个复制者(webdev:)还没有成功-我把我的工作分给了github&我要做一些不太复杂的事情。。。下一个测试是使用正在渲染的片段来显示字符串。在我知道这是可行的之后,我将创建railsCast的作者没有在其他模型上填充值的scaffold-以防它是一个空字段问题(我不认为ruby on rails是那么原始,但作为一个程序员,我们必须测试所有的理论,好吗?)。代码看起来不错。你的模特看起来怎么样?您是否有行的
接受\u嵌套\u属性\u?