response_with 重定向通知 Flash 消息不起作用 [英] respond_with redirect with notice flash message not working

查看:42
本文介绍了response_with 重定向通知 Flash 消息不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Rails 3.0.7.在控制器中我有:

I am using rails 3.0.7. In the controller I have:

  def create
    @subscription = Subscription\
      .new_from_nested_attributes_parameters(params[:subscription])

    if @subscription.save
      flash[:notice] = 'The Subscription was successfully created.'
    end

    respond_with @subscription
  end

并在视图中:

<%="Notice:#{flash[:notice]}"%>

尽管对象已正确保存,但不打印任何内容.

Doesn't print anything despite that the object is being properly saved.

你知道我应该如何解决这个问题吗?

Do you have an idea on how should I fix this?

推荐答案

我发现了问题.

flash[:notice]="...." 正在正确地处理创建操作,重定向到显示操作.

flash[:notice]="...." is properly working on the create action, redirecting to the show action.

我忘记了我的表演"包括要编辑的重定向.

What I forgot was that my 'show' consists on a redirect to edit.

我通过执行如下所示的 show 操作解决了这个问题:

I fixed this by implementing the show action like this:

def show
  redirect_to edit_subscription_path(@subscription),flash
end

从 Rails 3.1 开始,这应该通过:

From Rails 3.1 on, this should be accomplished with:

def show
  flash.keep
  redirect_to edit_subscription_path(@subscription)
end

这篇关于response_with 重定向通知 Flash 消息不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆