Ruby Sinatra的URL不断追加



我有四个按钮,每次单击按钮都会发送一个post请求(我将在下面显示我的代码(。

这是我的books controller.rb

post '/all?' do
@books = # Books here
haml :'books/index', locals: {books: @books}
end
post '/fantasy?' do
@fantasy = # Fantasy books here
haml :'books/index', locals: {fantasy: @fantasy}
end

这是我的index.haml文件:

%form{method: 'POST', action: "books/all"}
%input.btn.btn-md{type: 'submit', value: 'All Books'}
%form{method: 'POST', action: "books/fantasy"}
%input.btn.btn-md{type: 'submit', value: 'Fantasy'}

然而,会发生以下情况:(1( 网址:localhost:8080/books(2( 我点击all按钮(3( 网址:localhost:8080/books/all(4( 我点击fantasy按钮(5( 网址:localhost:8080/books/books/fantasy

这就是问题所在——我如何才能获得它,这样在第二次单击按钮时,它就会转到localhost:8080/books/fantasy?我确信这是一个简单的解决方案,只是不确定如何解决。谢谢

我认为推荐的方法是使用绝对路径并添加url助手。如果代码安装在子文件夹或反向代理后面,这应该会更好地经得起将来的考验。

%form{method: 'POST', action: url("/books/fantasy")}

最新更新