gpt4 book ai didi

ruby-on-rails - Rails : on redirect to same page form submitted from, 闪存消息未显示

转载 作者:行者123 更新时间:2023-12-01 19:34:50 26 4
gpt4 key购买 nike

在提交表单到 sexes_controller 中的“创建”操作时,我已经更改了一些内容以实际重定向回提交表单的页面 View /结果/索引,但现在即使我在做

<%= flash[:notice] %>

在 views/results/index 的底部(即提交表单的页面),这就是我认为您应该这样做的方式。

是不是因为正在发生某种缓存,所以没有显示 Flash 消息?知道如何解决这个问题吗?

更新

认为它可能更复杂,我尝试在结果 Controller 的索引操作中检索 flash 消息

@flashbash = Sex.find(params[:id])

然后回到views/results/index

<%= if @flashbash flash[:notice] %>  (I think this code is wonky)

请注意,我试过了,但没有用。它说,Couldn't find Sex without an ID

有什么办法可以解决这个问题吗?

最佳答案

通常,Flash 在应用程序的布局文件中呈现。这避免了在每个可能有闪现消息的 View 中重复输出 <%= flash[:notice] %>。

至于它为什么不显示,请检查您是否将 flash[:notice] 变量设置为要显示的内容。 Controller 中创建操作的示例可能如下所示:

# app/controllers/sex_controller.rb
def create
@sex = Sex.new(params[:sex])
if @sex.save
flash[:notice] = "Saved successfully"
redirect_to @sex # This redirects to the show action, where the flash will be displayed
else
flash[:error] = "There were errors..."
render :action => :new # This displays the new form again
end
end

# app/layouts/application.html.erb
<html>
...
<%= flash[:notice] %>
<%= flash[:error] %>
<%= yield %>
...
</html>

更多信息请点击此处:http://guides.rubyonrails.org/action_controller_overview.html#the-flash

关于ruby-on-rails - Rails : on redirect to same page form submitted from, 闪存消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com