views:

28

answers:

1

I have created a site which utilizes subdomains and searches whether or not the user is at: subdomain.domain.com or domain.com. If the user is in subdomain.domain.com, /views/layouts/application.html.erb appears, if the user is in domain.com /views/layouts/promo_site.html.erb appears. To accomplish this I closely followed Robby on Rails directions.

Both layouts utilize the same controller.

I've isolated the following problem:

  1. Controller logic is causing fail. "undefined method `orders' for nil:NilClass"
  2. If the controller is in the correct scope, subdomain.url.com the current_account method works fine. If it is in the url.com scope, the current_account method produces a nil. A full explanation of the current_account method is here.
  3. How do I use the controller under these conditions?

Example of utilization of the current_account method:

created_purchase_orders = current_account.orders.find(:all, :conditions => ["created_at >= ?", 3.days.ago], :order => "created_at DESC")

*the above code produces a nil under the url.com condition, and works fine in subdomain.url.com

Would something like [current_account.orders.find(:all, :conditions => ["created_at >= ?", 3.days.ago], :order => "created_at DESC")] unless current_account.nil? be a suggested fix?

+1  A: 

You cannot call .orders on nil so yes checking that first would be a good idea. I would suggest you do what you've said, minus the square brackets.

Controller:

@cpo = current_account.orders.find(:all, ...) unless current_account.blank?

Then in the view you can check whether @cpo is set with something like:

<%= render :partial => "current_purchase_order", :collection => @cpo if @cpo.present? %>
tsdbrown