How do I assign a value from params, or session, whichever exists?

Posted by irkenInvader on Stack Overflow See other posts from Stack Overflow or by irkenInvader
Published on 2010-05-13T15:18:56Z Indexed on 2010/05/13 15:24 UTC
Read the original article Hit count: 179

Filed under:
|

What is the "Rails-way" or the "Ruby-way" of doing the following:

In my controller, I'm creating and instance of an Options class. It will be initialized with information in the params hash if the params hash exists. Otherwise, it will check the sessions hash for the information. Finally, it will initialize with defaults if neither params nor session has the data it needs. Here is how I'm doing it now (it works fine, but it seems a little ugly):

if params[:cust_options]
  @options = CustomOptions.new( params[:cust_options] )
else
  if session[:cust_options
    @options = CustomOptions.new( session[:cust_options] )
  else
    @options = CustomOptions.new
  end
end

session[:cust_options] = @options.to_hash

Like I said, everything is working fine, I'm just looking for a more idiomatically Ruby way of writing this block of code.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby