How to render edit view and post flash message in rails3
In my account controller I'd like to display (render, redirect_to ?) the edit view after changes get saved and display flash notice.
def update
@account = Account.find(params[:id])
respond_to do 开发者_如何学Go|format|
if @account.update_attributes(params[:account])
format.html { redirect_to(@account, :notice => 'Account was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
By default you have to use a separate statement, e.g.
format.html {
flash[:notice] = 'message'
render :edit
}
This ticket has a patch to let you use render 'edit', :notice => 'message'
. It didn't get into Rails but there is a gem, flash_render, that adds it.
If you just use flash[:notice]
that value will still be available in the next request. Meaning, you'll see the text on the next 2 pages. Instead use flash.now
to only make the value available in the current request.
format.html {
flash.now[:notice] = 'message'
render :edit
}
For reference read Action Controller Overview 5.2.1
You can still use notices like in Rails 2:
flash[:notice] = "message"
Just add the following line to the top of your view to display it:
<p id="notice"><%= flash[:notice] %></p>
And you should use render
method if you don't want to make your users to fill edit forms once again.
精彩评论