简体   繁体   English

respond_with 重定向通知 flash 消息不起作用

[英]respond_with redirect with notice flash message not working

I am using rails 3.0.7.我正在使用导轨 3.0.7。 In the controller I have:在 controller 我有:

  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

and in the view:在视图中:

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

Doesn't print anything despite that the object is being properly saved.尽管 object 已正确保存,但不打印任何内容。

Do you have an idea on how should I fix this?你知道我应该如何解决这个问题吗?

I discovered the problem.我发现了问题。

flash[:notice]="...." is properly working on the create action, redirecting to the show action. flash[:notice]="...." 在 create 动作上正常工作,重定向到 show 动作。

What I forgot was that my 'show' consists on a redirect to edit.我忘记的是我的“节目”包括重定向到编辑。

I fixed this by implementing the show action like this:我通过执行这样的显示操作来解决这个问题:

def show
  redirect_to edit_subscription_path(@subscription),flash
end

From Rails 3.1 on, this should be accomplished with:从 Rails 3.1 开始,这应该通过以下方式完成:

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

In Rails 3.2, the following will work and appears to keep the flash intact:在 Rails 3.2 中,以下内容将起作用,并且似乎保持 flash 完好无损:

respond_with @subscription, :location => edit_subscription_path(@subscription)

You can skip the show page:您可以跳过显示页面:

Instead of:代替:

respond_with @subscription

Put:放:

respond_with @subscription, edit_subscription_path(@subscription)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM