0

投稿、コメント、質問のあるプロジェクトがあります。コメントは投稿に属し、質問はコメントに属します。ページのコメントに属するすべての質問を表示しようとしています。ただし、インデックス ページには質問が表示されません。エラーにはなりませんが、空白です。

ここに私のquestions_controller.rbがあります:

   class QuestionsController < ApplicationController
 before_action :set_question, only: [:show, :edit, :update, :destroy]


def index
  @comment = Comment.find params[:comment_id]
  @comment.questions
end


def show
end


def new
  @comment = Comment.find params[:comment_id]
end


def edit
end


def create
  @comment = Comment.find(params[:comment_id])
  @question = @comment.questions.create(question_params)

  respond_to do |format|
    if @question.save
      format.html { redirect_to comment_questions_path, notice: 'Question was successfully created.' }
    format.json { render action: 'show', status: :created, location: comment_questions_path }
  else
    format.html { render action: 'new' }
    format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end

インデックス ファイルは _question.html.erb パーシャルを呼び出します。

<%=div_for(@question) do %>
<%= @question.body %>
<% end %>

index.html.erb ファイル:

<%= render "questions/question" %>

最後に、インデックス ページへのリンクは次のようになります。

<%= link_to 'View Questions', comment_questions_path(comment)%>

私はチェックしましたが、質問はデータベースに保存されているので、それは問題ではありません。どんな助けでも本当に感謝しています。

4

2 に答える 2

0

コントローラーのどこにも @question 変数を定義しておらず、それをビューで使用しているため、空白になり、空白で表示されます。

question_controller.rb でこのコードを試してください

class QuestionsController < ApplicationController
 before_action :set_question, only: [:show, :edit, :update, :destroy]

  def index
    @comment = Comment.find params[:comment_id]
    @questions = @comment.questions
  end
  ...
end

ビューで @questions 変数を使用します。

于 2013-09-02T01:46:42.610 に答える