如果問題出現在消息視圖中,則需要包括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
嘗試@question而不是隻在您的形式 – bkdir 2014-08-27 15:58:55
@bkdir同樣的錯誤說「問題」。 – 2014-08-27 16:14:29