How to customize flash message based on success or failure with Inherited Resources Rails plugin?
Asked Answered
Y

2

1

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
Yolande answered 13/5, 2010 at 3:31 Comment(0)
L
2
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.

Ludwick answered 13/5, 2010 at 8:23 Comment(0)
M
3

Take a look at responders on its documentation.

Matildamatilde answered 13/5, 2010 at 8:11 Comment(0)
L
2
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.

Ludwick answered 13/5, 2010 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.