无效 POST 后切换区域设置

发布于 2024-11-26 09:05:45 字数 360 浏览 1 评论 0原文

我对以下问题感到困惑:在我的网络应用程序中,用户可以从下拉框中选择他们的语言。选择后,一段 Javascript 会获取当前 url,用新的区域设置字符串替换其中的旧区域设置子字符串,并将 window.location 设置为新的 url。只要 URL 是 GET 请求的结果,就可以很好地工作。

当用户发布表单并返回验证错误时,就会出现我的问题。该 url 现在是 POST url,(在我的例子中)不适用于 GET 请求。因此,如果用户现在决定切换语言,则会向服务器发送无效的 GET 请求。

有什么想法如何解决这个问题吗?

更具体一点:我在 Rails 3.0.9 的 Devise (1.4.2) 注册表单上遇到了这个问题

I'm puzzling on the following problem: in my web app users can select their language from a dropdown box. Upon selection, a piece of Javascript takes the current url, replaces the old locale substring in it with the new locale string and sets the window.location to the new url. That works great as long as the URL the result of a GET request.

My problem occurs when the user posts a form and it returns a validation error. The url is now a POST url, that (in my case) does not work with a GET request. Hence, if the user now decides to switch language, an invalid GET request will be sent to the server.

Any ideas how to solve this?

To be a little more specific: I'm running into this problem in Rails 3.0.9 on the registration form of Devise (1.4.2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽蝶幻影 2024-12-03 09:05:46

我经常编写自己的路线以避免您所描述的确切问题。我认为这是 Rails 路由的一个大缺陷。

您可以更改 Devise 路由以及其余资源路由,以便您的 GET 和 POST URL 看起来相同。这是我的意思的一个例子。

  scope '/posts' do
    get '/:id/edit' => "posts#edit", :as => "edit_posts"
    post '/:id/edit' => "posts#update", :as => "update_posts"
  end

不过,这样做需要做更多的工作。

您始终可以禁用出现问题的页面的区域设置选择。

I often write my own routes to avoid the exact problem you are describing. I think it's a big flaw in Rails routing.

You may be able to change the Devise routes, and the rest of your resource routes, so that your GET and POST URLs look identical. Here's an example of what I mean.

  scope '/posts' do
    get '/:id/edit' => "posts#edit", :as => "edit_posts"
    post '/:id/edit' => "posts#update", :as => "update_posts"
  end

It's a bit more work doing things that way, though.

You could always disable the locale select for the pages where you have problems.

ι不睡觉的鱼゛ 2024-12-03 09:05:46

您可以使用 javascript 检查页面上是否存在 .fieldWithError 类元素。如果是这样,您可以确定最后一个请求是 POST。在这种情况下,您必须更改表单的“action”属性(更改区域设置)并再次提交。

您无法使用 javascript 访问当前响应的 HTTP 方法。

You could check with javascript if there is a .fieldWithError classed element on the page. If so, you can be sure that the last request was POST. In that case, you would have to change the 'action' attribute for the form (change the locale) and submit it again.

You can not access the HTTP method of the current response with javascript.

小忆控 2024-12-03 09:05:46

@WizardofOgz 感谢您让我走上正轨。我添加这个答案,以便我可以粘贴一些代码,我不能在评论中粘贴这些代码,但我会接受你的答案。

我发现这篇专门针对 Devise 的相关帖子。我已经在密码重置路由上测试了它,这似乎工作正常(尽管我猜这还没有完成):

devise_for :user, :path_names => { :sign_up => "register", :sign_in => 'login', :sign_out => 'logout' }, :skip => [:passwords] do
  scope :controller => 'devise/passwords' do      
    post :create, :path => 'user/password/new', :as => :user_password
    get  :new, :path => 'user/password/new' , :as => :new_user_password
  end
end

@WizardofOgz thanks for putting me on the right track. I'm adding this answer, so that I can paste in some code, which I can't in a comment, but I'll accept your answer.

I found this related post specifically for Devise. I've tested it on the password reset routes and this seems to work fine (although I guess this isn't complete yet):

devise_for :user, :path_names => { :sign_up => "register", :sign_in => 'login', :sign_out => 'logout' }, :skip => [:passwords] do
  scope :controller => 'devise/passwords' do      
    post :create, :path => 'user/password/new', :as => :user_password
    get  :new, :path => 'user/password/new' , :as => :new_user_password
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文