Ruby-on-rails 通过闪存传递错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8252783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing error messages through flash
提问by alexs333
What is the best way to push error messages on redirect to?
在重定向时推送错误消息的最佳方法是什么?
I've previously used couple of approaches, but both of them has issue.
我以前使用过几种方法,但它们都有问题。
(1) Passing the entire object with error on flash and using error_messages_for:
(1) 在 flash 上传递有错误的整个对象并使用 error_messages_for:
def destroy
if @item.destroy
flash[:error_item] = @item
end
redirect_to some_other_controller_path
end
I found that this method causes cookie overflows.
我发现这个方法会导致cookie溢出。
(2) Passing a single error message:
(2) 传递单个错误信息:
def destroy
if @item.destroy
flash[:error] = @item.full_messages[0]
end
redirect_to some_other_controller_path
end
This way I only send a single error message, what if there are many? Does Anyone knows a better way?
这样我只发送一个错误消息,如果有很多怎么办?有谁知道更好的方法?
回答by Matthew Rudy
Firstly, you can achieve what you're trying to do by setting a single sentence.
首先,您可以通过设置单个句子来实现您想要做的事情。
flash[:error] = @item.errors.full_messages.to_sentence
I think you could also set it as the array without overflowing the cookie.
我认为您也可以将其设置为数组而不会溢出 cookie。
flash[:error] = @item.errors.full_messages
But as the other guys say, flash is generally better off being used to return specific messages.
但正如其他人所说,Flash 通常最好用于返回特定消息。
eg.
例如。
flash[:error] = "We were unable to destroy the Item"
A common pattern is as such.
一个常见的模式是这样的。
def some_action
if @record.action
flash[:notice] = "Action performed successfully"
redirect_to @record
else
flash[:error] = "Action failed"
render :action => "some_action"
end
end
Namely, we have two paths.
也就是说,我们有两条路径。
- Action succeeds. We redirect.
- Action fails. We show a page, flash an error, and have
@record.errorson hand to callerror_messages_for(@record)if we so wish.
- 行动成功。我们重定向。
- 动作失败。我们显示一个页面,显示一个错误,如果我们愿意,可以
@record.errors随时调用error_messages_for(@record)。
回答by Alok Swain
Flashis a part of the Rails session which is cleared among requests and Rails session is implemented using cookies. (atleast until Rails-2). Cookiesgenerally are used to store very minimal amount of data as the maximum amount the default cookie can store is 4 kbs i think. So storing the entire model objects might not be a very good option. To do that you can use a different cookie store which would allow you to store large amount of data.
Flash是 Rails 会话的一部分,它在请求中被清除,Rails 会话是使用 cookie 实现的。(至少在 Rails-2 之前)。Cookies通常用于存储非常少量的数据,因为我认为默认 cookie 可以存储的最大数量是 4 kbs。所以存储整个模型对象可能不是一个很好的选择。为此,您可以使用不同的 cookie 存储来存储大量数据。
As for the second problem, you can store as many error messages in the flash variable. The way you did flash[:error], you can do the same and store other messages as well using other keys to store other messages.
至于第二个问题,您可以在 flash 变量中存储尽可能多的错误消息。按照您的方式flash[:error],您可以执行相同的操作并存储其他消息以及使用其他键来存储其他消息。
Hope this helps.
希望这可以帮助。
回答by tonatiuhnb
This is what worked for me:
这对我有用:
@item.errors.messages.map { |k,v| v }.join('<br>').html_safe
回答by Adam Spiers
One of the answers to Rails validation over redirectsuggests using a new clone_with_errorsto do a shallow clone of the model's object instance. I guess that might help stay below the 4k limit; however I can imagine that for complex models there could still be problems, e.g. see this page written in 2007 which advocates against storing model object instances in the session.
Rails 对重定向进行验证的答案之一建议使用 newclone_with_errors对模型的对象实例进行浅层克隆。我想这可能有助于保持在 4k 限制以下;但是我可以想象,对于复杂的模型,仍然可能存在问题,例如,请参阅2007 年编写的此页面,该页面主张反对在会话中存储模型对象实例。
回答by HeeL
Since flash[:error] is a hash, you can pass error messages to it with an "<<" operator
由于 flash[:error] 是一个散列,您可以使用“<<”运算符将错误消息传递给它

