Including a blank in a dropdown

damn, i’m still seeing the same error with your suggestion…

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 something else I guess.

I used…

class WelcomeController < ApplicationController
def index

unless params[:location][:id] || 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

and went to

http://localhost:3000/

parameters = none

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 [email protected]

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

Or’s short-circuit on the first true in the ‘if’ case, but are you sure
it works as a full inverse in the ‘unless’ case?

If it’s: unless (condA || condB), which makes sense to me, you’re out of
luck.

If condA is false, it WILL go after condB…

Rewrite it as an ‘if’

On May 11, 4:01 am, Ar Chron [email protected] wrote:

Rewrite it as an ‘if’
yup, expanding de morgan’s laws in your head are a recipe for
disaster. (and don’t forget about the blank? method).

Fred

On 10/05/2009, at 5:12 AM, bingo bob <rails-mailing-list@andreas-
s.net> wrote:

Ok, get that, but I still don’t see it.

I’ve truggled with these before - sigh. I don’t think select helpers
are
that helpful really - I dont find them very easy.

Oh come on they’re much nicer than rolling the HTML yourself! You just
have to learn the syntax same as you had to learn the HTML syntax
oriinally when you started learning HTML.

Blog: http://random8.zenunit.com/
Learn: http://sensei.zenunit.com/
Twitter: http://twitter.com/random8r

bingo bob,

you just wanna use

unless params[:location][:id].blank?

location is set

else

no location at all

end

in your controller like Fred above suggested.

blank? is lifesaver.

On May 11, 1:58 pm, Frederick C. [email protected]

Thanks for all these comments!

However somethings else maybe going on… it’s failing and I don’t know
why.

Even with this code.

class WelcomeController < ApplicationController
def index

  unless params[:location][:id].blank?
    # location is set
    @teachers = Teacher.all
    @locations = Location.all

  else
    # no location set
    @teachers = Teacher.all
    @locations = Location.all

  end

end

end

it fails with

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.[]

Read up on form_tag api.rubyonrails.org/classes/ActionView/Helpers/
FormTagHelper.html

Fix your form accordingly.

Once you have done that, and once u have the select box looking as you
want it, choose a location and hit submit. Check the development log
for the parameters hash and dump it here. Or, you could do

raise params.to_yaml in the first line of you ‘index’ action. This
will output all the contents of the params hash onto the webpage. Copy
that and put it here. Just put the content of the params when you
submit the form, here.

On May 11, 4:13 pm, bingo bob [email protected]

post your complete error code (with file and lines included). Then
someone could help you.

tom

bingo bob wrote:

You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz

Here you go…

NoMethodError in WelcomeController#index

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.[]
RAILS_ROOT: /Users/rupe/Sites/teachers

Application Trace | Framework Trace | Full Trace
/Users/rupe/Sites/teachers/app/controllers/welcome_controller.rb:5:in
index' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:1322:insend’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:1322:in
perform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/filters.rb:617:incall_filters’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/filters.rb:610:in
perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/benchmark.rb:17:in
ms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/benchmark.rb:10:inrealtime’
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.2/lib/active_support/core_ext/benchmark.rb:17:in
ms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/rescue.rb:160:in
perform_action_without_flash' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/flash.rb:141:inperform_action’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:523:in
send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:523:inprocess_without_filters’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/filters.rb:606:in
process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:391:inprocess’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/base.rb:386:in
call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/routing/route_set.rb:433:incall’
Request

Parameters:

None
Show session dump

Response

Headers:

{“Content-Type”=>"",
“Cache-Control”=>“no-cache”}

Now because the parameters are empty, the form really isnt working. do
take
a look at the API. Implement form_tag or form_for for your needs and
make it
work. Check if you’ve got the parameters. And go from there…

On Mon, May 11, 2009 at 9:46 PM, bingo bob
<[email protected]

wrote:

/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/filters.rb:617:in

`perform_action_without_flash’
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.2/lib/action_controller/filters.rb:606:in
Request
{“Content-Type”=>“”,
“Cache-Control”=>“no-cache”}

Posted via http://www.ruby-forum.com/.


In Sport We Trust !!!

Ah. Maybe this is the problem.

The form is on the index page.
The form is not submitted when the user goes to this index page.

The form is only submitted when they click the go button.

How do I handle that?

Vinay S. wrote:

Now because the parameters are empty, the form really isnt working. do
take
a look at the API. Implement form_tag or form_for for your needs and
make it
work. Check if you’ve got the parameters. And go from there…

I think…
REPLACE
unless params[:location][:id].blank?
#set location

WITH
unless params[:location].blank?
#set location

Remember, the location id that the user will submit IF he uses the form
will
still be in params[:location][:id]. So if you’re doing a find in there
using
the ID then you can access it by using params[:location][:id].

A better check would be to check the commit message that gets passed
when
the user submits the form. In this case, if your form submit button says
“Go”, then

unless params[:commit] == “Go”
#set location

On Tue, May 12, 2009 at 12:07 AM, bingo bob <
[email protected]> wrote:

Vinay S. wrote:


In Sport We Trust !!!

Vinay - you nailed it. Many thanks.

I ended up with my controller like this…

#===========

class WelcomeController < ApplicationController
def index

# finding all the locations for the drop down
@locations = Location.all


  if params[:commit] == "go" && params[:location][:id].to_i >= 1
    # location is set AND it's not a blank location id so find the 

teachers in the location
@location = Location.find(params[:location][:id])
@teachers = @location.teachers
else
# no form submitted so find just find all teachers
@teachers = Teacher.all
end

end

end

#=========

Here’s my view code…

<% title “Welcome” %>

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.


<% form_tag do %>

<%= select(“location”, “id”, Location.all.collect {|l| [ l.name, l.id
]}, {:include_blank => true}) %>
<%= submit_tag “go” %>

<% end -%>


<% if @teachers %>
<% for teacher in @teachers %>
<%= link_to [teacher.first_name + ’ ’ + teacher.last_name], teacher
%>

<%= teacher.email %>

Location: <%= teacher.location.name %>

Languages:
<% for language in teacher.languages %>
<%= language.name %>
<% end %>



<% end %>
<% end %>

#============

Works great.

I’m interested to see any critique of this code? all comments
appreciated.
Is there a better way to handle the && params[:location][:id].to_i >= 1
it’s the way I thought to do it?

Many thanks to all for the comments here - very useful.

finding all the locations for the drop down

@locations = Location.all

You dont need this in the controller because you’re finding all
locations in
the view itself.

Is there a better way to handle the && params[:location][:id].to_i >=
1 it’s the way I thought to do it?
&& !(params[:location][:id].blank?)

or

!(params[:location].blank?)

whatever works for you. You can see i love blank? :slight_smile:

Ideally, this whole functionality should go into the model.

Create a teacher.full_name method. Its simple.

On Tue, May 12, 2009 at 4:41 PM, bingo bob
<[email protected]

wrote:

   # no form submitted so find just find all teachers

Here’s my view code…
sunt in culpa qui officia deserunt mollit anim id est laborum.
<% end -%>
<% for language in teacher.languages %>
I’m interested to see any critique of this code? all comments
appreciated.
Is there a better way to handle the && params[:location][:id].to_i >= 1
it’s the way I thought to do it?

Many thanks to all for the comments here - very useful.

Posted via http://www.ruby-forum.com/.


In Sport We Trust !!!