如何使用 Inherited Resources Rails 插件根据成功或失败自定义 Flash 消息?

发布于 2024-09-01 16:59:07 字数 1035 浏览 7 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没企图 2024-09-08 16:59:07

查看其文档中的responders

Take a look at responders on its documentation.

涫野音 2024-09-08 16:59:07
success.html {flash[:notice] = "Hurray!"; redirect_to(@article)}}

failure.html do 
  flash[:notice] = "All bad..."
  render :action => "show"
end

只需两种方法即可实现。

success.html {flash[:notice] = "Hurray!"; redirect_to(@article)}}

failure.html do 
  flash[:notice] = "All bad..."
  render :action => "show"
end

Just two ways of doing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文