respond_with redirect with notice flash message not working
I am using rails 3.0.7. In the controller I have:
def create
@subscription = Subscription\
.new_from_nested_attributes_parameters(params[:subscri开发者_StackOverflowption])
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.
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.
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:
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:
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)
精彩评论