【发布时间】:2012-10-03 15:23:09
【问题描述】:
在我的控制器中,我有以下内容
post "/buy_item" do
redirect '/login' unless session[:name]
@owner = Market::User.user_by_name(params[:owner])
@item = @owner.item_by_id(params[:item].to_i)
@current_user = Market::User.user_by_name(session[:name])
if @current_user.buy_item?(@item)
@current_user.buy_item(@item)
else
redirect '/error'
end
end
当我强制使用“else”时,我会被正确重定向到/error,但之后我会立即被重定向到另一个页面 (/?loggedin=true)。 redirect "/?loggedin=true" 是 post "/login" 方法的最后一行。所以,它似乎在以某种方式调用POST /login..
/error 的路由如下所示:
get "/error" do
redirect '/login' unless session[:name]
template = ERB.new File.new($VIEWS_FOLDER + "/error.erb").read, nil, "%"
template.result(binding)
end
/error.erb 中的任何内容都没有重定向,当我直接调用 localhost:4567/error 时,它不会被重定向。
这是日志:
127.0.0.1 - - [03/Oct/2012 17:15:03] "POST /login HTTP/1.1" 303 - 0.0012
localhost - - [03/Oct/2012:17:15:03 CEST] "POST /login HTTP/1.1" 303 0
localhost:4567/login -> /login
127.0.0.1 - - [03/Oct/2012 17:15:03] "GET /?loggedin=true HTTP/1.1" 200 3916 0.0055
localhost - - [03/Oct/2012:17:15:03 CEST] "GET /?loggedin=true HTTP/1.1" 200 3916
localhost:4567/login -> /?loggedin=true
127.0.0.1 - - [03/Oct/2012 17:15:05] "POST /buy_item HTTP/1.1" 303 - 0.0030
localhost - - [03/Oct/2012:17:15:05 CEST] "POST /buy_item HTTP/1.1" 303 0
localhost:4567/?loggedin=true -> /buy_item
127.0.0.1 - - [03/Oct/2012 17:15:05] "GET /error HTTP/1.1" 200 1609 0.0039
localhost - - [03/Oct/2012:17:15:05 CEST] "GET /error HTTP/1.1" 200 第1609章
localhost:4567/?loggedin=true -> /error
127.0.0.1 - - [03/Oct/2012 17:15:05] "GET /?loggedin=true HTTP/1.1" 200 3916 0.0063
localhost - - [03/Oct/2012:17:15:05 CEST] "GET /?loggedin=true HTTP/1.1" 200 3916
localhost:4567/login -> /?loggedin=true
【问题讨论】: