控制器找不到删除路线,但方法在控制器中

时间:2019-03-31 19:03:44

标签: http-delete sinatra-activerecord

我正在构建一个小型的sinatra应用程序,但遇到了一个问题。找不到我的DELETE控制器功能。

ApplicationController

delete '/stories/:id' do
        if logged_in?
            @story = Story.find_by_id(params[:id])
        if  @story && @story.user_id == current_user.id
            params.delete("_method")
            #binding.pry
            @story.destroy
            redirect to "/users/show"
        else
            redirect to "/session/login"
        end
    end
end

删除表单操作

Title: <%= @story.title %><br>
Story: <%= @story.content %><br>


<form action="/stories/<%= @story.id %>" method="POST"> 
<input type="hidden" id="hidden"  name="_method" value="DELETE">
<input type="submit" value="Remove Story">
</form>

错误

class ApplicationController
  delete '/stories/13' do
    "Hello World"
  end
end

我也不敢尝试这条路线

1 个答案:

答案 0 :(得分:0)

您需要使用以下将转换的中间件 <input type="hidden" id="hidden" name="_method" value="DELETE">到http delete

use Rack::MethodOverride