redirect_to destination, :notice => “Thanks for your comments!”

Posted: February 1, 2008 in ノート, Rails, RESTful, Technology
Tags: , , , , ,

Were you bored of writing flash[:notice] = “blah blah blah” or flash[:error] = “gya gya gya you got error dammit!” then redirect users to anywhere you want?

If you answered “YES” or nod in your heart then continue else close your window browser and go away from this blog! I’m bloody boring and you’ve been warned!

Were you bored of writing a protected method redirect_to_index(msg = nil) and then you put a code flash[:notice] = msg unless msg.nil? and then you redirect users to anywhere you want?

If you answered “YES” or nod in your heart then continue else close your window browser and go away from this blog! I’m bloody boring and you’ve been warned!

Alright, now comes this quick solution with unrevealed weaknesses from my poor eyes damn I still can’t sleep it’s 4:30 in the morning in Kuala Lumpur already :-(

    def redirect_to(destination, msg = {})
      msg[msg.keys[0]].any? && flash[msg.keys[0]]=(msg[msg.keys[0]])
      redirect_to destination and return
    end

so you can just call this protected method from your method this way:

redirect_to note_url(@note), :notice => "Thanks for your comment!"

or you can just call this protected method from your protected method like so:

redirect_to notes_url, :error => "Note is not found!"

Yes, it was a protected method, cos it’s called from rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found.

Now you tell me about any weaknesses from overriding redirect_to above. See you around.

Comments
  1. This redirection idea was against flexible write codes that write codes philosophy, it’s only suitable for unflexible ruby redirection that doesn’t have such redirect_to status something (for example any number), and any other rails defaults options.

tinggalkan feedback konstruktif atau lebih baik diam

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s