Rendering a nested partial template - 在两个单独的模型上渲染嵌套表单
Rendering a nested partial template - to rended a nested form on a two separate models
我在这个问题的第二周,最近使用了 railsCast #196 (revised)。我知道这是旧的 - 也许那是我的问题。作为额外的旋转,我将在 Cloud 9 之外托管我的 rails 服务器。
我已经尝试按照几个不同的教程进行学习,这就是我所了解的。奇怪的是 none 他们的句法与 rails 文档中的官方 ruby 匹配...... Rails 查看模板。
在 railsCast 中,这个人能够显示空白字段...我不确定如何...所以我还没有设法填充问题或答案字段.我什至不确定这两个 rails 控制台消息是什么意思 - 而且那里没有记录。
感谢阅读和任何建议!
-M
事不宜迟,我的 senario ... 通过模板嵌套表单,如 railsCast 196 ...
所示
我的 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 []>
我的终端控制台日志...
Started GET "/surveys/5/edit" for 68.54.21.200 at 2015-11-27 02:46:48 +0000
Cannot render console from 68.54.21.200! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SurveysController#edit as HTML
Parameters: {"id"=>"5"}
Survey Load (0.4ms) SELECT "surveys".* FROM "surveys" WHERE "surveys"."id" = ? LIMIT 1 [["id", 5]]
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 5]]
Rendered surveys/_form.html.erb (4.2ms)
Rendered surveys/edit.html.erb within layouts/application (7.3ms)
Completed 200 OK in 70ms (Views: 67.9ms | ActiveRecord: 0.5ms)
所以我的代码...
surveys_controller.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
编辑操作视图
<% 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 %>
_question_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 %>
_answer_fields.html.erb
<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
我在这个问题的第二周,最近使用了 railsCast #196 (revised)。我知道这是旧的 - 也许那是我的问题。作为额外的旋转,我将在 Cloud 9 之外托管我的 rails 服务器。
我已经尝试按照几个不同的教程进行学习,这就是我所了解的。奇怪的是 none 他们的句法与 rails 文档中的官方 ruby 匹配...... Rails 查看模板。
在 railsCast 中,这个人能够显示空白字段...我不确定如何...所以我还没有设法填充问题或答案字段.我什至不确定这两个 rails 控制台消息是什么意思 - 而且那里没有记录。
感谢阅读和任何建议!
-M
事不宜迟,我的 senario ... 通过模板嵌套表单,如 railsCast 196 ...
所示我的 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 []>
我的终端控制台日志...
Started GET "/surveys/5/edit" for 68.54.21.200 at 2015-11-27 02:46:48 +0000
Cannot render console from 68.54.21.200! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by SurveysController#edit as HTML
Parameters: {"id"=>"5"}
Survey Load (0.4ms) SELECT "surveys".* FROM "surveys" WHERE "surveys"."id" = ? LIMIT 1 [["id", 5]]
Question Load (0.2ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 5]]
Rendered surveys/_form.html.erb (4.2ms)
Rendered surveys/edit.html.erb within layouts/application (7.3ms)
Completed 200 OK in 70ms (Views: 67.9ms | ActiveRecord: 0.5ms)
所以我的代码...
surveys_controller.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
编辑操作视图
<% 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 %>
_question_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 %>
_answer_fields.html.erb
<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