为什么我在讨论中得到“NameError”?

时间:2013-06-22 17:26:13

标签: ruby-on-rails ruby nameerror

我目前正在开发一个Ruby on Rails应用程序,该应用程序涉及用户创建讨论和使用微博进行评论。这是一个非常简单的概念,但我是新手,遇到了一些麻烦。

当试图查看讨论页面(讨论索引)时,我收到错误“讨论中的NameError #index”:

undefined local variable or method `discussion' for #<#<Class:0x00000100c6e020>:0x0000010380edd8>

这是我的讨论控制器:

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表格:

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

这是我的讨论部分:

<% 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控制器:

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 Discussion < ActiveRecord::Base
  attr_accessible :content, :intro
  has_many :microposts, dependent: :destroy

  belongs_to :user

  validates :content, presence: true, length: { maximum: 600 }
  validates :intro, presence: true

  default_scope order: 'discussions.created_at DESC'
end

这是我的用户模型:

class User < ActiveRecord::Base
  attr_accessible :email, :username, :age, :sex, :points, :password, :password_confirmation
  has_secure_password
  has_many :microposts, dependent: :destroy
  has_many :discussions, dependent: :destroy


  before_save {|user| user.email = email.downcase}
  before_save :create_remember_token

  validates :username, presence: true, length: {maximum: 15}
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
  validates :age, presence: true
  validates :sex, presence: true
  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true

  private

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end

end

这是我的讨论指数:

<% provide(:title, "Discussions") %>
<h1>Discussions</h1>
<ul class = "discussions">
  <% @discussions.each do |discussion| %>
      <li>
        <%= render :partial =>"discussions/discussion", :locals=>{:discussion=>discussion} %>
      </li>
  <% end %>
</ul>

1 个答案:

答案 0 :(得分:0)

尝试重写此行:

<%= render 'shared/micropost_form', :locals => {:discussion => discussion }%>

为:

<%= render 'shared/micropost_form', :discussion => discussion %>