用于#<ActionDispatch :: Request的未定义方法`flash'
我有Ruby on Rails 3的东西
我尝试这个简单的代码
def index flash[:notice] = "ok" respond_to do |format| format.html # index.html.erb end end
这是行不通的
NoMethodError in DashboardsController#index undefined method `flash' for #
当我尝试
redirect_to :some_in, :notice => "ok"
在其他地方(在some_controller.rb中),然后打印这个:.erb中的通知我有相同的错误,未定义的方法`flash’
我坚持这个。 我使用谷歌搜索它,但它没有帮助。
在你的应用config/applications.rb
的config/applications.rb
中添加此项
config.api_only = false
请在您的config / application.rb文件中包含ActionDispatch :: Flash中间件。
config.middleware.use ActionDispatch::Flash
这可能对你有所帮助。
flash[:notice]
只会出现在redirect_to
或render
(尽管你需要flash.now[:notice]
)。
闪存用于向用户提供关于用户采取的动作的状态的反馈。 仅渲染索引通常不属于此类别,因为它仅显示数据,而不显示用户采取操作的结果。
举个例子:
def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to(@post, :notice => 'Post was successfully created.') } else format.html { render :action => "new" } end end end
在这种情况下,只有在直接保存post后,闪光灯才会出现在Post Post视图中。