Response_with redirect with flash message notification not working

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

  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 view:

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

Does not print anything even though the object is correctly saved.

Do you have an idea on how to fix this?

+3
source share
3 answers

I have discovered a problem.

flash [: notice] = "...." works correctly on the create action, redirecting the show action.

I forgot that my "show" consists of redirecting to editing.

I fixed this by doing the show action as follows:

def show
  redirect_to edit_subscription_path(@subscription),flash
end

From Rails 3.1 on, this should be done with

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

In Rails 3.2, the following will work and will be displayed:

respond_with @subscription, :location => edit_subscription_path(@subscription)
+2
source

:

:

respond_with @subscription

:

respond_with @subscription, edit_subscription_path(@subscription)
+1

All Articles