如何在Rails中使用respond_to设置自定义flash

Nic*_*nto 5 respond-to ruby-on-rails-3

respond_to你可以设置flash[:notice]这样的

respond_to do |format|
  format.html { redirect_to photo_path(photo), :notice => 'The photos was saved') }
  format.xml  { render :xml => photo, :status => :created}
end
Run Code Online (Sandbox Code Playgroud)

我试图设置flash [:success] :success => "yay"但它不起作用.

难道我做错了什么?

小智 8

您应该以不同方式使用redirect_to:

redirect_to photo_path(photo), :flash => { :success => "Yeepee!" }
Run Code Online (Sandbox Code Playgroud)

您可以直接使用的唯一闪光灯是

  • :注意
  • :警报
  • :错误

希望有所帮助


小智 5

从轨道4,你可以直接使用:successredirect_to.

只需添加以下行:

# in app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
    [...]

    add_flash_types :error, :success, :info

    [...]
Run Code Online (Sandbox Code Playgroud)

没有这一行,在respond_to中,:notice会产生flash,但是:成功不起作用.

帽子提示米兰蒙达尔的帖子!