【问题标题】:Rendering a nested partial template - to rended a nested form on a two separate models渲染嵌套的部分模板 - 在两个单独的模型上渲染嵌套表单
【发布时间】:2016-10-09 04:39:01
【问题描述】:

我在本期的第二周,最近使用了 railsCast #196 (revised)。我知道这是旧的 - 也许这是我的问题。作为一个额外的旋转,我将我的 Rails 服务器托管在 Cloud 9 之外。

我已经尝试遵循几个不同的教程来学习一个,这就是我所掌握的。奇怪的是,它们的语法与官方 ruby​​ on rails 文档中的语法都不匹配……Rails View 模板。

在 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>

【问题讨论】:

  • 这不是一个答案,这是一个建议。对嵌套表单使用“茧”宝石。它很容易实现并且有很好的文档。
  • 我将剥离它的另一个实例并 fork github,但我真的很想了解有关 Rails 故障排除的足够了解,以便我可以亲自体验 Rails 的“魔力”——我不知道不想成为一个copypasta webdev :)
  • 还没有成功——我在 github 上分叉了我的工作,我打算做一些不太复杂的事情……下一个测试是使用部分渲染来显示字符串。在我知道这可行之后,我将创建 railsCast 的作者没有制作的脚手架来填充其他模型上的值 - 以防它是一个空字段问题(我不认为 ruby​​ on rails 是那个原始的,但作为程序员,我们必须测试所有的理论,对吧?)。
  • 代码看起来不错。你的模型看起来如何?你有accepts_nested_attributes_for 行吗?

标签: ruby-on-rails templates nested-forms partials


【解决方案1】:

在我从网络上运行的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    相关资源
    最近更新 更多