2014-08-27 60 views
0

如果问题出现在消息视图中,则需要包括Answers窗体的帮助。我得到一条NameError: undefined local variable or method `question'在线= form_for(:question, :url => question_answers_path(question)) do |f|NameError未定义的局部变量或方法

如果它有助于消息来自对话控制器。

/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 

原始形式的代码工作就是里面的“答案“文件夹(我试图让下面的代码在”messages“文件夹中工作):

<%= form_for(:question, :url => question_answers_path(@message.question)) do |f| %> 
    <ul> 
     <li><%= f.text_area :answer, {:placeholder => 'Please add your response...'}%></li> 
     <li><%= f.text_field :recipient_id, {:placeholder => 'Please add your name...'} %></li> 
     <li><%= f.submit "Respond" %></li> 
     </ul> 
     <% end %> 

路线:

resources :questions do 
    resources :answers, only: [:new, :create] 
    end 
+0

尝试@question而不是只在您的形式 – bkdir 2014-08-27 15:58:55

+0

@bkdir同样的错误说“问题”。 – 2014-08-27 16:14:29

回答

0

你应该尝试更新的代码,如:

- 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| 
+0

谢谢。但是我仍然试图克服NameError,但是我会为if语句保留这种修改。 – 2014-08-27 17:13:43

+0

@CorneliusWilson您是否尝试过不要混淆“if”语句并在两行上进行操作? – 2014-08-27 17:19:58

+0

检查更新回答 – RAJ 2014-08-27 17:20:05

0

的问题是要传递question到路径帮手,但它是不确定的。您应该使用@message.question

question_answers_path(@message.question) 
+0

我在这里发布之前曾尝试过,它给出了同样的错误。我只是仔细检查再确认。我原来的代码在切换到SLIM之前,我正在使用不同的视图文件夹,它是'<%= form_for(:question,:url => question_answers_path(@ message.question))do | f | %>'并且该代码仍然适用于我的应用程序(只是不在我的新视图中)。 – 2014-08-27 19:51:17

相关问题