Ruby on rails NameError未定义的局部变量或方法

Ruby on rails NameError未定义的局部变量或方法,ruby-on-rails,Ruby On Rails,如果消息视图中存在问题,我需要帮助包括答案表单。我在(:question,:url=>question\u-answers\u-path(question))do | f行中得到一个NameError:undefined局部变量或方法'question' 如果有帮助,则消息来自对话控制器 /messages/_form.html.slim: | if question = @message.question.present? = form_for(:question, :url => qu

如果消息视图中存在问题,我需要帮助包括答案表单。我在(:question,:url=>question\u-answers\u-path(question))do | f行中得到一个
NameError:undefined局部变量或方法'question'

如果有帮助,则消息来自对话控制器

/messages/_form.html.slim:

| if question = @message.question.present?
= form_for(:question, :url => question_answers_path(question)) do |f|
  ul
    li= f.text_area :answer,  placeholder=('Please add your response...') 
    li= f.text_field :recipient_id,  placeholder=('Please add your name...') 
    li= f.submit "Respond"

    | else
    = form_for :message, url: [:reply, conversation] do |f|
        = f.text_area :body, rows: 4, style: 'width: 95%'
        br
        = f.submit "Send Message", class: 'btn btn-primary'
        = submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger'
主计长答覆:

  def new
    @question = Question.find(params[:question_id])
  end

  def show
    @question = Question.find(params[:question_id])
    @answer = Answer.find(params[:id])
  end

  def create
    @question = Question.find(params[:question_id])
    if @question.update_attributes(params[:question])
      redirect_to questions_path
    else
      render :new
    end
  end
end
   def show
      @question = Question.find(params[:id])
      @questions = Question.order("created_at DESC")
      respond_with(@questions)
    end

    def create
      @question = Question.new(params[:question])
      if @question.save
        @message = current_user.send_message(@question.recipient, @question.question, "You have a question from #{@question.sender_id}") 
        redirect_to :back, notice: 'Your question was saved successfully. Thanks!'
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end
  end
问题:

  def new
    @question = Question.find(params[:question_id])
  end

  def show
    @question = Question.find(params[:question_id])
    @answer = Answer.find(params[:id])
  end

  def create
    @question = Question.find(params[:question_id])
    if @question.update_attributes(params[:question])
      redirect_to questions_path
    else
      render :new
    end
  end
end
   def show
      @question = Question.find(params[:id])
      @questions = Question.order("created_at DESC")
      respond_with(@questions)
    end

    def create
      @question = Question.new(params[:question])
      if @question.save
        @message = current_user.send_message(@question.recipient, @question.question, "You have a question from #{@question.sender_id}") 
        redirect_to :back, notice: 'Your question was saved successfully. Thanks!'
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end
  end
在“answers”文件夹中工作的原始表单代码(我正在尝试让下面的代码在“messages”文件夹中工作):


您应该尝试更新如下代码:

- question = @message.question # will avoid making multiple calls to db for same record
| if question.present?
= form_for(:question, :url => question_answers_path(question)) do |f|

问题是您正在将
question
传递到路径帮助器中,但它未定义。您应该使用
@message。问题

question_answers_path(@message.question)

试试@question,而不是在你的表单中说“question”,谢谢。不过,我仍在努力克服名称错误,但我会记住对if语句的修改。@CorneliusWilson您是否尝试过将
if
语句解混并在两行上执行它?@RAJ。。。它为nil:NilClass提供了
未定义的方法'question'。如果我做了
@question
,页面会工作,但这将导致路由错误
没有路由匹配[POST]“/questions/answers”
@CorneliusWilson向我显示您的
消息\u controller.rb
@RAJ。。。messages controller addedI在发布到这里之前尝试过这个方法,但它给出了相同的错误。我只是再确认一下。在切换到SLIM之前,我的原始代码是
question\u answers\u path(@message.question))do | f |%>
,我的应用程序仍然可以使用这些代码(只是不在我的新视图中)。