Rails:在redirect_to之后丢失嵌入式表单的闪存/错误
这是一个愚蠢的例子,但我试图理解 Rails 中的幕后传递方式。可能有一种更好的“Rails”方法来解决这个问题......如果是这样,请告诉我。我有:
Warren has_many Rabbits
Rabbit belongs_to Warren
我列出现有的兔子,然后提供用于添加新兔子的表格。
warrens/show.html.haml
%h1 This Warren
%h2 Rabbits
= render @warren.rabbits, :target => @warren
%h2 Add a rabbit:
= render "rabbits/form", :parent => @warren, :foreign_key => :warren_id
= link_to 'Edit', edit_warren_path(@warren)
\|
= link_to 'Back', warrens_path
它有效。我可以在观看沃伦的表演视图时添加和消灭兔子。问题是 flash 消息(:success
、:notice
)和/或 rabbit.errors
是未显示在视图上/视图中。现在,我不在我的 warren.rb 模型中使用 accepts_nested_attributes_for
。但这大部分都有效。学习 Rails,很想知道发生了什么以及如何让我的消息顺利流动...
rabbits/_form.html.haml
= form_for ([parent, parent.rabbits.build]) do |f|
= render "shared/error_messages", :target => parent
.field
= f.label :name
= f.text_field :name
.field
= f.label :color
= f.text_field :color
.field
= f.hidden_field foreign_key
.actions
= f.submit 'Save'
layouts/application.html.haml< /strong>
!!!
%html
%head
...
%body
.container
- flash.each do |key, value|
%div{ :class => ['flash', key] }= value
= yield
rabbits_controller.rb
class RabbitsController < ApplicationController
def create
@warren = Warren.find(params[:warren_id])
@rabbit = @warren.rabbits.create(params[:rabbit])
redirect_to @warren
end
def destroy
@warren = Warren.find(params[:warren_id])
@rabbit = @warren.rabbits.find(params[:id])
@rabbit.destroy
redirect_to @warren
end
shared/_error_messages.html.haml
-if target.errors.any?
#error_explanation
%h2= "#{pluralize(target.errors.count, "error")} prohibited this record from being saved:"
%ul
- target.errors.full_messages.each do |msg|
%li= msg
This is a stupid example but I'm trying to understand how thing get passed around behind the scenes in Rails. And there's probably a better, "Rails", way to approach this...if so please let me know. I have:
Warren has_many Rabbits
Rabbit belongs_to Warren
I'm listing existing rabbits and then providing a form for adding a new rabbit.
warrens/show.html.haml
%h1 This Warren
%h2 Rabbits
= render @warren.rabbits, :target => @warren
%h2 Add a rabbit:
= render "rabbits/form", :parent => @warren, :foreign_key => :warren_id
= link_to 'Edit', edit_warren_path(@warren)
\|
= link_to 'Back', warrens_path
It works. I can add and destroy rabbits while looking at the Warren's show view. The problem is that the flash messages (:success
, :notice
) and/or the rabbit.errors
aren't being shown on/in the view. Now, I am not using accepts_nested_attributes_for
in my warren.rb model. but this is mostly working. Learning Rails so wondering what's going on and how to get my messages flowing smoothly...
rabbits/_form.html.haml
= form_for ([parent, parent.rabbits.build]) do |f|
= render "shared/error_messages", :target => parent
.field
= f.label :name
= f.text_field :name
.field
= f.label :color
= f.text_field :color
.field
= f.hidden_field foreign_key
.actions
= f.submit 'Save'
layouts/application.html.haml
!!!
%html
%head
...
%body
.container
- flash.each do |key, value|
%div{ :class => ['flash', key] }= value
= yield
rabbits_controller.rb
class RabbitsController < ApplicationController
def create
@warren = Warren.find(params[:warren_id])
@rabbit = @warren.rabbits.create(params[:rabbit])
redirect_to @warren
end
def destroy
@warren = Warren.find(params[:warren_id])
@rabbit = @warren.rabbits.find(params[:id])
@rabbit.destroy
redirect_to @warren
end
shared/_error_messages.html.haml
-if target.errors.any?
#error_explanation
%h2= "#{pluralize(target.errors.count, "error")} prohibited this record from being saved:"
%ul
- target.errors.full_messages.each do |msg|
%li= msg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您没有在控制器中设置闪烁消息。尝试以下操作:
或者,我相信您可以使用
:success
/notice
:希望这会有所帮助!
更新
对于错误消息,您需要类似
target.error_messages
的内容。您可以在此处。Seems that you are not setting the flash messages in your controller. Try the following:
Alternatively, I believe you can use
:success
/notice
:Hope this helps!
UPDATE
For your error messages, you need something like
target.error_messages
. You can find more details on customizing the output here.