在AJAX帖子停止在远程计算机上工作之后的redirect_to

时间:2016-10-06 18:40:08

标签: ruby-on-rails ajax redirect

我有一个“创建”动作,由AJAX调用。然后重定向到另一个动作。在我的本地机器上,一切正常。以下是本地计算机的日志文件:

Started POST "/tasks" for 127.0.0.1 at 2016-10-06 20:08:44 +0200
Processing by TasksController#create as JS

Started GET "/roles/refresh_blank_page?cursor=3988&focus=true" for 127.0.0.1 at 2016-10-06 20:08:44 +0200
Processing by RolesController#refresh_blank_page as JS

但是在远程机器上,在开发或生产中,它打破了“ActionController :: UnknownFormat”。格式突然似乎是HTML,正如您在日志中看到的那样:

Started POST "/tasks" for 88.217.180.75 at 2016-10-06 20:13:00 +0200
Processing by TasksController#create as JS

Started GET "/roles/refresh_blank_page?cursor=3981&focus=true" for 88.217.180.75 at 2016-10-06 20:13:02 +0200
Processing by RolesController#refresh_blank_page as HTML

以下是我的行动:

def create
  (...)
  redirect_to roles_refresh_blank_page_path(focus: true, cursor: @task.id)
end


def refresh_blank_page
  (...)
  respond_to do |format|
    format.js { render :file => "/roles/refresh_blank_page.js.erb" }
  end
end

我在这里阅读了很多帖子,并在重定向中添加了“format:'js'”(也试过“remote:true”)

redirect_to roles_refresh_blank_page_path(focus: true, cursor: @task.id, format:'js')

在我现在看到的日志中:

Started GET "/roles/refresh_blank_page.js?cursor=3983&focus=true" for 88.217.180.75 at 2016-10-06 20:19:12 +0200
Processing by RolesController#refresh_blank_page as JS

但现在在RolesController#refresh_blank_page中产生“ActionController :: InvalidCrossOriginRequest”

我比较了我本地机器和远程机器上的宝石。远程计算机上的某些宝石具有更高的版本号。在这两种情况下,我都运行rails 4.1.8和ruby 2.1.5p273。

感谢您的帮助, 罗恩

1 个答案:

答案 0 :(得分:0)

我通过使用bundle package将所有开发宝石打包到供应商/缓存中来解决这个问题。现在它可以在远程机器上运行。

比照Whats the difference between bundle install --deployment and bundle pack