You can't call "empty?" method on a nil object. You must check that it
exists first. This is super simple in Ruby. Just try and call the
object, if it's nil it evaluates false, if it exists, it is true:

  unless params[:location][:id] || params[:location][:id].empty?

Precendence will short circuit on a nil first and never try and call
the empty? method. But If the params[:location][:id] exists, then you
CAN check for empty?

On May 10, 12:09 pm, bingo bob <rails-mailing-l...@andreas-s.net>
wrote:
> That looks great to me, so I'm now uber confused.
>
> The error I get is this...
>
> You have a nil object when you didn't expect it!
> You might have expected an instance of ActiveRecord::Base.
> The error occurred while evaluating nil.[] (it's erroring on the unless
> params[:location][:id].empty? line)
>
> with no params.
>
> I implemented the following.
>
> class WelcomeController < ApplicationController
>   def index
>
>       unless params[:location][:id].empty?
>         # location is set, so find it!
>         @location = Location.find(params[:location][:id])
>       else
>         # location is empty, so just get all the teachers
>         @teachers = Teacher.all
>       end
>
>   end
>
> end
>
> <% title "Welcome" %>
>
> <p>Find me in app/views/welcome/index.html.erb</p>
>
> <p>
> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
> tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
> veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
> commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
> velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
> occaecat cupidatat non proident, sunt in culpa qui officia deserunt
> mollit anim id est laborum.
> </p>
>
> <hr>
>
> <% form_tag do %>
>
>   <%= select("location", "id", Location.all.collect {|l| [ l.name, l.id
> ]}, {:prompt => 'Any Location'}) %>
>   <%= submit_tag "go" %>
>
> <% end -%>
>
> <%= render :inline => params.inspect %>
>
> <hr>
>
> <% if @teachers %>
>   <% for teacher in @teachers %>
>     <%= link_to [teacher.first_name + ' ' + teacher.last_name], teacher
> %><br>
>     <%= teacher.email %><br>
>     Location: <%= teacher.location.name %><br>
>     Languages:
>     <% for language in teacher.languages %>
>     <%= language.name %>
>     <% end %>
>
>     <hr>
>   <% end %>
>
> <% end %>
>
> routes.
>
> ActionController::Routing::Routes.draw do |map|
>   map.resources :languages
>
>   map.resources :locations
>
>   map.resources :teachers, :member => {:no_more_photo => :put}
>
>   map.login 'login', :controller => 'teacher_sessions', :action => 'new'
>   map.logout 'logout', :controller => 'teacher_sessions', :action =>
> 'destroy'
>
>   map.resources :teacher_sessions
>
>   # The priority is based upon order of creation: first created ->
> highest priority.
>
>   # Sample of regular route:
>   #   map.connect 'products/:id', :controller => 'catalog', :action =>
> 'view'
>   # Keep in mind you can assign values other than :controller and
> :action
>
>   # Sample of named route:
>   #   map.purchase 'products/:id/purchase', :controller => 'catalog',
> :action => 'purchase'
>   # This route can be invoked with purchase_url(:id => product.id)
>
>   # Sample resource route (maps HTTP verbs to controller actions
> automatically):
>   #   map.resources :products
>
>   # Sample resource route with options:
>   #   map.resources :products, :member => { :short => :get, :toggle =>
> :post }, :collection => { :sold => :get }
>
>   # Sample resource route with sub-resources:
>   #   map.resources :products, :has_many => [ :comments, :sales ],
> :has_one => :seller
>
>   # Sample resource route with more complex sub-resources
>   #   map.resources :products do |products|
>   #     products.resources :comments
>   #     products.resources :sales, :collection => { :recent => :get }
>   #   end
>
>   # Sample resource route within a namespace:
>   #   map.namespace :admin do |admin|
>   #     # Directs /admin/products/* to Admin::ProductsController
> (app/controllers/admin/products_controller.rb)
>   #     admin.resources :products
>   #   end
>
>   # You can have the root of your site routed with map.root -- just
> remember to delete public/index.html.
>   # map.root :controller => "welcome"
>   map.root :controller => "welcome"
>
>   # See how all your routes lay out with "rake routes"
>
>   # Install the default routes as the lowest priority.
>   # Note: These default routes make all actions in every controller
> accessible via GET requests. You should
>   # consider removing the them or commenting them out if you're using
> named routes and resources.
>   #map.connect ':controller/:action/:id'
>   #map.connect ':controller/:action/:id.:format'
> end
>
> ======
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to