新手问题 [已解决] Completed 302 Found,页面不跳转

killpigman · 2016年01月27日 · 最后由 qinfanpeng 回复于 2016年01月27日 · 6402 次阅读

页面不跳转,在 js 里输出返回值,是我要跳转的 html。

def create
    @imprint = Imprint.new(imprint_params)
    @imprint.save
    redirect_to @imprint
end

def show
    @imprint = Imprint.find(params[:id])
end
Started POST "/imprints/" for 127.0.0.1 at 2016-01-27 11:08:53 +0800
Processing by ImprintsController#create as */*
  Parameters: {"imprint"=>{"lng"=>"114.005017", "lat"=>"22.59508", "accuracy"=>"65", "text"=>"事实上"}}
   (0.7ms)  BEGIN
  SQL (0.5ms)  INSERT INTO `imprints` (`lng`, `lat`, `accuracy`, `text`, `created_at`, `updated_at`) VALUES (114.005017, 22.59508, 65, '事实上', '2016-01-27 03:08:53', '2016-01-27 03:08:53')
   (7.9ms)  COMMIT
Redirected to http://localhost:3000/imprints/24
Completed 302 Found in 59ms (ActiveRecord: 22.8ms)

Started GET "/imprints/24" for 127.0.0.1 at 2016-01-27 11:08:53 +0800
Processing by ImprintsController#show as */*
  Parameters: {"id"=>"24"}
  Imprint Load (0.4ms)  SELECT  `imprints`.* FROM `imprints` WHERE `imprints`.`id` = 24 LIMIT 1
  Rendered imprints/show.html.erb within layouts/application (0.3ms)
Completed 200 OK in 165ms (Views: 162.2ms | ActiveRecord: 0.4ms)

你是通过 ajax 请求 ImprintsController#create 的?

Redirected to http://localhost:3000/imprints/24

不都已经跳转了么?看日子,目标页面也被请求了,是你的前端出问题了吧,浏览器没变化?

#1 楼 @qinfanpeng 是的,$.post 会有什么问题?

#2 楼 @huacnlee 地址栏和浏览器都没有变化。

Ajax 请求怎么会跳转呢...

6 楼 已删除

#3 楼 @killpigman 参考@huacnlee的回复。如果你真要通过 Ajax 请求的话,建议 create 里用 json 返回待跳转的地址,然后在 javascript 里用 Turbolinks.visit url_to_redirect来实现。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号