Discussions中的NameError #index

时间:2013-06-21 15:16:30

标签: ruby-on-rails ruby nomethoderror

我正在尝试编写一个红宝石应用程序,让您发布讨论和评论帖子。问题是我无法得到评论 - 这里称为微博,以显示在讨论之下。现在,我在讨论中得到错误NameError #index:未定义的局部变量或#<#:0x0000010563a1e0>的方法`discussion'。有什么想法吗?

微博形式

<% @micropost = Micropost.new %>

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

微博控制器

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

讨论控制器

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

讨论视图

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

感谢您的帮助,我确定它的内容非常简单。我很擅长这个

2 个答案:

答案 0 :(得分:0)

在此代码中,

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

看起来@user.discussion是你的问题。应该是@user.discussions。与下一行@user.microposts

类似

答案 1 :(得分:0)

我想你用show方法进行讨论。但是,在show方法和除create之外的控制器中的其他位置,您声明@discussions

我实际上无法得到,为什么在show方法中你做了更适合索引方法的东西,但无论如何,Rails将变量从控制器传递给视图,如果它被声明为实例(使用@)。

因此,要在视图中定义变量,您应该在相对控制器方法中使用@discussion,并且本地人也应该通过@discussion

:locals => {:discussion => @discussion }