【问题标题】:How to customize flash message based on success or failure with Inherited Resources Rails plugin?如何使用 Inherited Resources Rails 插件根据成功或失败自定义 Flash 消息?
【发布时间】:2010-05-13 03:31:24
【问题描述】:

我在 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

【问题讨论】:

    标签: ruby-on-rails ruby resources ruby-on-rails-plugins inherited


    【解决方案1】:

    查看responders 的文档。

    【讨论】:

      【解决方案2】:
      success.html {flash[:notice] = "Hurray!"; redirect_to(@article)}}
      
      failure.html do 
        flash[:notice] = "All bad..."
        render :action => "show"
      end
      

      只有两种方法。

      【讨论】:

        猜你喜欢
        • 2019-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-13
        • 2012-05-16
        • 1970-01-01
        相关资源
        最近更新 更多