如果消息视图中存在问题,我需要帮助包括答案表单。我在第NameError: undefined local variable or method `question'
行收到= form_for(:question, :url => question_answers_path(question)) do |f|
。
如果它有助于消息来自Conversations控制器。
/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
在“answers”文件夹中工作的原始表单代码(我试图让以下代码在“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 :(得分: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|
答案 1 :(得分:0)
问题是您将question
传递给路径助手,但未定义。您应该使用@message.question
:
question_answers_path(@message.question)