如何使用 Inherited Resources Rails 插件根据成功或失败自定义 Flash 消息?
我在 2.3.5 Rails 应用程序中使用继承的资源插件,并且想知道如何根据创建和更新操作中的成功或失败来更改 flash[:notice] (或任何其他 flash)。
因此,鉴于以下情况,如果成功,如何添加 flash[:notice] = "All good" ... 如果失败,如何添加 flash[:notice] = "All bad"?
谢谢
class ArticleController < InheritedResources::Base
actions :show, :create, :update
respond_to :html, :json
before_filter :authorize_upsert, :only => [:create, :update]
def create
#init new game
@article = Article.new
set_article_attributes_from_app
@article.is_published = params[:article_publish_to_web] || false
@ article.game_source = @client_application
create! do |success, failure|
success.html {redirect_to(@article)}
success.json {render :json => {:id=>@article.id, :created_at=>@article.created_at, :picture_urls=> @article.assets.map { |a| root_url.chop + a.photo.url}}}
failure.html {render :action => "show"}
failure.json {render :json=>@article.errors, :status => :unprocessable_entity}
end
end
I'm using the inherited resources plugin in a 2.3.5 Rails application and was wondering how to change the flash[:notice] (or any other flash) based on the success OR failure in my create and update actions.
So given the below, how do I add flash[:notice] = "All good" if success ... and flash[:notice] = "All bad" if failure?
Thanks
class ArticleController < InheritedResources::Base
actions :show, :create, :update
respond_to :html, :json
before_filter :authorize_upsert, :only => [:create, :update]
def create
#init new game
@article = Article.new
set_article_attributes_from_app
@article.is_published = params[:article_publish_to_web] || false
@ article.game_source = @client_application
create! do |success, failure|
success.html {redirect_to(@article)}
success.json {render :json => {:id=>@article.id, :created_at=>@article.created_at, :picture_urls=> @article.assets.map { |a| root_url.chop + a.photo.url}}}
failure.html {render :action => "show"}
failure.json {render :json=>@article.errors, :status => :unprocessable_entity}
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看其文档中的responders。
Take a look at responders on its documentation.
只需两种方法即可实现。
Just two ways of doing it.