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




<li>

   <div class = "intro-bar"><span class = "intro"><%=discussion.intro
%></span></div>

   <div class = "content-bar">

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

       <div class = "vote-plus"><%= link_to "Stay Together",
signup_path,class: "btn btn-large btn-staytogether" %></div>

     </div>

   </div>

</li>


<span class = "timestamp">

     Posted <%= time_ago_in_words(discussion.created_at) %> ago.

   </span>


<div class = "comments">

   <% discussion.microposts.each do |micropost|%>

       <li>

         <div class = "post-comment"><%= micropost.content%></div>

       </li>

   <% end %>

</div>


<% 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 %>
     <div class="field">
       <%= f.text_area :content, placeholder: "Compose new micropost..." %>
     </div>
     <%= f.hidden_field :discussion_id, discussion.id%>

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




thanks!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/971e8844-ba71-4371-b354-1c868553212c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Colin Law at Jun 22, 2013 at 8:19 am

    On 21 June 2013 20:17, wrote:
    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>
    You have failed to show us two vital things, firstly which line of
    code the error occurs at, look in the server terminal window and you
    should see the full message and call stack. It should tell you which
    line of your code is generating the error. Secondly you have not told
    us the relations between the models (has_many, belongs_to etc). If
    you do not understand what that means then I suggest starting with a
    good tutorial such as railstutorial.org (free to use online) which
    will show you the basics of rails. In fact I suggest working right
    through a tutorial before starting your own app even if you do
    understand what I mean. Also read (and make sure you understand) the
    Rails Guides.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvDcGMfupG%3DV65GanphsSx3iXv_Y4WQX7txEw6OpEWP%3DQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Aguardientico at Jun 22, 2013 at 5:27 pm
    Change this line:
    @discussions = @user.discussion.paginate(page: params[:page])

    for

    @discussions = @user.discussion*s*.paginate(page: params[:page])

    El viernes, 21 de junio de 2013 14:17:31 UTC-5, ni...@relesh.com escribió:
    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 %>




    <li>

    <div class = "intro-bar"><span class = "intro"><%=discussion.intro
    %></span></div>

    <div class = "content-bar">

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

    <div class = "vote-plus"><%= link_to "Stay Together",
    signup_path,class: "btn btn-large btn-staytogether" %></div>

    </div>

    </div>

    </li>


    <span class = "timestamp">

    Posted <%= time_ago_in_words(discussion.created_at) %> ago.

    </span>


    <div class = "comments">

    <% discussion.microposts.each do |micropost|%>

    <li>

    <div class = "post-comment"><%= micropost.content%></div>

    </li>

    <% end %>

    </div>


    <% 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 %>
    <div class="field">
    <%= f.text_area :content, placeholder: "Compose new micropost..." %>
    </div>
    <%= f.hidden_field :discussion_id, discussion.id%>

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




    thanks!
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3bb124eb-f280-4686-b079-741057743a9e%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJun 22, '13 at 5:43a
activeJun 22, '13 at 5:27p
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

3 users in discussion

Nick: 1 post Colin Law: 1 post Aguardientico: 1 post

People

Translate

site design / logo © 2021 Grokbase