Hi everybody!
I’m currently in the process of writing my first ruby on rails app. I’m
quite new to the whole thing and I could use some help with an issue
i’ve
been stuck on for a while.
The goal here is to crease a discussion board where users can comment
with
microposts. Very simple. The issue is that I am getting the error
NameError in Discussions#index
undefined local variable or method `discussion’ for
#<#Class:0x00000103eb4e08:0x00000103e78bb0>
I’m sure the fix is quite simple. Here’s the relevant code
discussion controller
class DiscussionsController < ApplicationController
before_filter :signed_in_user, only: [:index, :edit, :update]
def show
@user = User.find(params[:id])
@discussions = @user.discussion.paginate(page: params[:page])
@microposts = @user.micropost.paginate(page: params[:page])
end
def index
@discussions = Discussion.all
end
def create
@discussion = current_user.discussions.build(params[:discussion])
if @discussion.save
flash[:success] = "Discussion Started!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
def edit
end
def update
end
def new
end
end
micropost controller
class MicropostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def index
end
def create
@discussion = current_user.discussions.new
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Posted!"
redirect_to root_url
else
render 'static_pages/home'
end
end
def destroy
end
end
discussion board
<% content_for :script do %>
<%= javascript_include_tag 'hover_content' %>
<% end %>
<span class = "content"><%= discussion.content %></span>
<div class = "buttons">
<div class = "vote-neg"><%= link_to "Break Up", signup_path,class:
“btn btn-large btn-breakup” %>
<div class = "vote-plus"><%= link_to "Stay Together",
signup_path,class: “btn btn-large btn-staytogether” %>
</div>
Posted <%= time_ago_in_words(discussion.created_at) %> ago.
<% discussion.microposts.each do |micropost|%>
<li>
<div class = "post-comment"><%= micropost.content%></div>
</li>
<% end %>
<% if signed_in? %>
<div class = "row">
<aside class = "span4">
<section>
<%= render 'shared/micropost_form', :locals => {:discussion =>
discussion }%>
</section>
</aside>
</div>
<% end %>
micropost form partial
<% @micropost = Micropost.new %>
<% @micropost.discussion_id = discussion.id %>
<%= form_for(@micropost) do |f| %>
<%= render ‘shared/error_messages’, object: f.object %>
<%= f.text_area :content, placeholder: “Compose new micropost…”
%>
<%= f.hidden_field :discussion_id, discussion.id%>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
thanks!