Rails:在redirect_to之后丢失嵌入式表单的闪存/错误

发布于 2024-10-07 01:39:45 字数 2276 浏览 3 评论 0原文

这是一个愚蠢的例子,但我试图理解 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 技术交流群。

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

发布评论

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

评论(1

薄暮涼年 2024-10-14 01:39:45

看来您没有在控制器中设置闪烁消息。尝试以下操作:

def create
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.create(params[:rabbit])
  redirect_to @warren, :flash => { :info => "Rabbit was just born." }
end

def destroy
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.find(params[:id])
  @rabbit.destroy
  redirect_to @warren, :flash => { :info => "Rabbit was brutally murdered!" }
end

或者,我相信您可以使用 :success / notice

redirect_to @warren, :success => "Rabbit was brutally murdered!"

希望这会有所帮助!

更新

对于错误消息,您需要类似 target.error_messages 的内容。您可以在此处。

Seems that you are not setting the flash messages in your controller. Try the following:

def create
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.create(params[:rabbit])
  redirect_to @warren, :flash => { :info => "Rabbit was just born." }
end

def destroy
  @warren = Warren.find(params[:warren_id])
  @rabbit = @warren.rabbits.find(params[:id])
  @rabbit.destroy
  redirect_to @warren, :flash => { :info => "Rabbit was brutally murdered!" }
end

Alternatively, I believe you can use :success / notice:

redirect_to @warren, :success => "Rabbit was brutally murdered!"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文