当我需要PATCH时,Rails表单运行POST

时间:2016-12-23 01:26:23

标签: ruby-on-rails forms

我有一个rails表单,我想通过运行更新操作(PATCH)来编辑答案,但我一直收到此错误:

No route matches [POST] "/answers/724"

这是表格:

<%= form_for :answers, url: { action: :update } do |f| %>
<%= f.label(@answer.question.question_text) %>
<%= f.text_area(@answer.answer_text) %>


<div class="form-group">
  <%= f.submit "Submit", class: "btn btn-success" %>
</div>

<% end %>

控制器:

def edit
    @answer = Answer.find_by(id: params[:id])
  end

  def update
    @answer = Answer.find_by(id: params[:id])
    if @answer.update(answer_text: params[:answer][:answer_text])
      redirect_to '/answers/edit'
      flash[:success] = "Answer Updated"
    else
      render 'edit'
    end
  end

如何让此表单执行PATCH请求而不是POST?

1 个答案:

答案 0 :(得分:0)

您需要明确地将方法传递给form_for

form_for :answers, url: { action: :update }, html: { method: :patch } do |f|