2014-08-27 88 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

相關問題