0

私は現在、ユーザーがディスカッションを作成し、マイクロポストでコメントすることを含む Ruby on Rails アプリケーションに取り組んでいます。これは非常に単純な概念ですが、私は新しく、いくつかの問題に遭遇しました。

ディスカッション ページ (ディスカッションのインデックス) を見ようとすると、"NameError in Discussions#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.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 %>

これは私のマイクロポストコントローラーです:

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

1 に答える 1