1

我们有一个由多个控制器的render :new操作创建的表单,我们在创建操作中重用该表单以显示验证错误消息。我相信这是 simple_form 和验证的方法。纠正我,如果我在这里错了。

我们还有一个通用的语言切换机制,它重定向到 current_url,具有不同的语言环境。

问题:在验证失败和新表单的第二次呈现后,语言选择会引发错误(在此处发布会非常误导)。问题是创建操作需要经过验证的对象,我们的语言选择不会再次将其传递给当前 url。

你将如何解决这个问题?

我们可以尝试教我们的语言切换器“创建”并让它发送另一个具有相同参数的帖子请求,但这看起来很糟糕。我们的小助手中必须有很多逻辑,我们将在哪里存储对象(至少其中一种根本不持久)?有人提到(ab-)使用 Flash 消息重新创建对象,但它是一个巨大的表单,最多有 50 个验证,而且我猜这个尺寸更难看。在这些情况下将对象存储在会话中并让助手再次发布对象(如果存在)可能会起作用。我最喜欢这个,但感觉也差得很远。

我们可以尝试让 simple_form 使用“new”动作而不是仅仅渲染“new”,但这看起来真的很糟糕。

我们可以完全禁用创建操作的语言切换,并发出警告说这一步必须以所选语言完成。

你有什么意见,其他建议吗?我将不胜感激。

谢谢,安迪

4

1 回答 1

0

So we changed the language helper to send the same post request again, if it is on a page created by a POST. It ended up looking like this. Not a lot of code added:

def language_link(language)
  url_options = { locale: language }
  if request.request_method == 'POST'
    link_to(language, url_for(params.merge(url_options)), method: :post)
  else
    link_to(language, url_for(url_options))
  end
end

We were carefully making sure we don't end up sending valid data a second time. Creating a second payment, or a second order would be quite bad here for example. We need to keep this in mind in the future as well, when we're creating new post routes accessible on a part of our application where language is changeable. That's the main problem here.

It does not consider PUT requests now because we don't have any edit/update functionality on the part of the app where language is selectable.

We can live with this version in our code. So I post this as an answer. But I'd still be happy to see a better (less dangerous) version, our any thoughts on this at all.

Cheers,
Andy

于 2013-05-28T10:19:34.753 回答