I am trying to add a polling feature to the app from Hartl's Rails Tutorial. Started off by following #196 Nested Model Form and Nested forms with Rails 4.2 Strong Parameters. After submitting the form, the content saves properly to the database, however, I am unable to see the results in the show page and cannot figure out why.
Models-
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
Controllers-
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
Routes-
Rails.application.routes.draw do
resources :microposts, only: [:create, :destroy]
resources :polls
resources :answers
end
Views-
_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>
I think this should be all of the relevant information, but please let me know if I am missing anything. Thank you very much for any help at all!