嵌套表单不在显示页面上显示提交的内容

Nested form not displaying submitted content on show page

我正在尝试向来自 Hartl 的 Rails 教程的应用程序添加轮询功能。从关注 #196 Nested Model Form and Nested forms with Rails 4.2 Strong Parameters 开始。提交表单后,内容正确保存到数据库中,但是,我无法在显示页面中看到结果,也不知道为什么。

型号-

class Micropost < ActiveRecord::Base
  has_many :polls, dependent: :destroy
  accepts_nested_attributes_for :polls, :reject_if => lambda {|a| a[:content].blank? } 

  def questions_for_form
    collection = polls.where(micropost_id: id)
    collection.any? ? collection : polls.build
  end
end

class Poll < ActiveRecord::Base
  belongs_to :micropost
  has_many :answers, dependent: :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda {|a| a[:content].blank? }

  def answers_for_form
    collection = answers.where(micropost_id: id)
    collection.any? ? collection : answers.build
  end
end

class Answer < ActiveRecord::Base
  belongs_to :poll
end

控制器-

class StaticPagesController < ApplicationController
  def home
    if logged_in?
      @micropost  = current_user.microposts.build
      @poll = @micropost.polls.build
        3.times {@poll.answers.build}
      @feed_items = current_user.feed.paginate(page: params[:page])
    end
  end
end

class MicropostsController < ApplicationController
  def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

  private

    def micropost_params
      params.require(:micropost).permit(:content, polls_attributes: [:content, :id, :micropost_id, answers_attributes: [:content, :id, :poll_id]])
    end
end

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
  end
end

路线-

Rails.application.routes.draw do
  resources :microposts,          only: [:create, :destroy]
  resources :polls
  resources :answers
end

观看次数-

_micropost_form.html.erb

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new post..." %>
  </div>

  <button type="button" class="btn btn-default btn-xs" data-toggle="collapse" data-target="#pollcollapse" id="pollbtn">Poll</button>

  <div id="pollcollapse" class="collapse">
  <%= f.fields_for :polls do |questions_for_form| %>
    <%= render 'shared/poll_fields', :f => questions_for_form %>
  <% end %>
  </div>

  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

_poll_fields.html.erb

<div class="field">
  <%= f.text_field :content, placeholder: "Poll question..."  %>
</div>

<%= f.fields_for :answers do |answers_for_form| %>
  <%= render 'shared/answer_fields', :f => answers_for_form %>
<% end %>

_answer_fields.html.erb

<div>
  <%= f.text_field :content, placeholder: "Answer" %>
</div>

show.html.erb

<% provide(:title, @user.name) %>
<div class="row">
  <aside class="col-md-4">
    <section class="user_info">
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </section>
  </aside>
  <div class="col-md-8">
    <% if @user.microposts.any? %>
      <h3>Posts (<%= @user.microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>
</div>

_micropost.html.erb

<li id="micropost-<%= micropost.id %>">

  <span class="content"><%= micropost.content %></span>

  <span>
    <% for poll in @micropost.polls %>    //Poll & answer content do not appear.
        <%= poll.content %>
        <% for answer in poll.answers %>
          <%= answer.content %>
        <% end %>
    <% end %>
  </span>

</li>

我认为这应该是所有相关信息,但如果我遗漏了什么,请告诉我。非常感谢您的帮助!

在您的 _micropost.html.erb 部分中,您引用了 @micropost,它似乎没有在任何地方定义。尝试将其从实例变量更改为局部变量 micropost(多次了解 @micropostmicropost here). Also, in Ruby it's recommended by people smarter than me (at least I've read it 之间的区别),您应该使用 .each而不是 for。您部分中的更新代码:

<li id="micropost-<%= micropost.id %>">

  <span class="content"><%= micropost.content %></span>

  <span>
    <% micropost.polls.each do |poll| %>
        <%= poll.content %>
        <% poll.answers.each do |answer| %>
          <%= answer.content %>
        <% end %>
    <% end %>
  </span>

</li>

此外,由于您在展示操作中定义了@microposts,因此您可以像这样在展示视图中重构代码:

<div class="col-md-8">
    <% if @microposts.any? %>
      <h3>Posts (<%= @microposts.count %>)</h3>
      <ol class="microposts">
        <%= render @microposts %>
      </ol>
      <%= will_paginate @microposts %>
    <% end %>
  </div>

是实例与局部变量问题。我想说使用 gen 可能更容易,但如果你正在学习 rails 并且你有时间,我的意见是第一次自己做更好。当您 运行 进入没有 gem 的东西时,它可以帮助您学习框架并更好地解决问题。了解自己之后,如果您发现 gem 确实符合您的目的,请使用它……或者制作您自己的 gem!干杯。