如果问题出现在消息视图中,则需要包括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