Including a blank in a dropdown


#1

Been trying all morning - daft thing ARRRRRGH!

I’d like to have the top option in my drop down to be “please select”
and if that’s the case All teachers should be shown.

I can’t get the Please Select to work though ?


view

<% title “Welcome” %>

Find me in app/views/welcome/index.html.erb

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 %>
Filter on location:
<%= select_tag “location_id”,
options_from_collection_for_select(@all_locations, :id, :name, selected
= @location.id), :prompt => “Please Select” %>

<%= 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 %>

<hr>

<% end %>

<% end %>

controller

class WelcomeController < ApplicationController
def index

@all_locations = Location.all
@location = Location.find(params[:location_id].to_i) if

params[:location_id]

if @location
  @teachers = @location.teachers
else
  @teachers = Teacher.all
end

end
end


#2

rails api doc

select(“post”, “person_id”, Person.all.collect {|p| [ p.name, p.id ]
}, {:prompt => ‘Select Person’})

tom

bingo bob wrote:


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do

<%= submit_tag “Go” %>

  @teachers = Teacher.all
end

end
end

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

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


#3

Can’t get that working unfortunately.


#4

you have to use select() instead of select_tag()

or merge your options_from_collection_for_select with the “empty”

please select

tom

bingo bob wrote:

Can’t get that working unfortunately.

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

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


#5

well this

<%= select “location_id”,
options_from_collection_for_select(@all_locations, :id, :name, selected
= @location.id), :prompt => “Please Select” %>

just gives me a drop down with the word prompt in it.

still stumped unfortunately.


#6

bingo bob wrote:

well this

<%= select “location_id”,
options_from_collection_for_select(@all_locations, :id, :name, selected
= @location.id), :prompt => “Please Select” %>

just gives me a drop down with the word prompt in it.

Look again at Tom’s example. Notice that he has one more argument than
you do. You either need to add back the first argument or use
form_for @object do |f|
f.select …
end

Don’t expect the same results as Tom if you’re not doing the same thing.

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#7

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.


#8

bingo bob wrote:

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

What don’t you see? Look closely at the differences as I explained.

Best,
Marnen


#9

would you be kind enough to show me a worked example of hwo to do it
like you suggest…

with

form_for @object do |f|
f.select …
end

That would be extremely helpful.


#10

OK, Well I did what I did like this - it works. It’s horribly complex
for what it is though, I don’t mind the Array for now as I can
understand that part, I’d be very grateful to simplify the IF statements
though.

You can see what I need to do I hope - just have a drop down with a No
Filter option, the user should return to the index page after the drop
down with the selected city still highlighted - as I say this works but
it looks horrible and unsure how to do it right?

thanks

VIEW SAMPLE

<% form_tag do %>

<% cities_array = [[“No Filter”, 0]] + Location.all.map { |city|
[city.name, city.id] } %>
<% # cities_array.inspect %>

<% if params[:location_id] == “0” || params[:location_id] == nil %>
<%= select_tag (:location_id, options_for_select(cities_array, 0))%>
<% else %>
<%= select_tag (:location_id, options_for_select(cities_array,
@location.id))%>
<% end %>

<%= 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 %>

<hr>

<% end %>

<% end %>

CONTROLLER

class WelcomeController < ApplicationController
def index

@all_locations = Location.all

if params[:location_id] == "0" || params[:location_id] == nil # No 

Filter Selected - ID 0
@location = nil
@teachers = Teacher.all
else
@location = Location.find(params[:location_id])
@teachers = @location.teachers
end

end
end


#11

well ok,

this alone gives me the dropdown i need.

<%= select(“location”, “location_id”, Location.all.collect {|l| [
l.name, l.id ]}, {:prompt => ‘Select Location’}) %>

But how do I then submit, I need to somehow link that up with a way to
submit it.


#12

bingo bob wrote:

well ok,

this alone gives me the dropdown i need.

<%= select(“location”, “location_id”, Location.all.collect {|l| [
l.name, l.id ]}, {:prompt => ‘Select Location’}) %>

But how do I then submit, I need to somehow link that up with a way to
submit it.

Just submit the form like any other form you’ve ever created.

Best,
Marnen


#13

bingo bob wrote:

OK, Well I did what I did like this - it works. It’s horribly complex
for what it is though,
[…]

Don’t go to all that trouble! Just follow Tom’s example. As I
explained before, he has 4 arguments – call them a, b, c, and d. You
can either do

select a, b, c, d

or use a form_for block and do

f.select b, c, d

as already explained. You already have a worked example thanks to
Tom, so use it!

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#14

bingo bob wrote:
[…]

What about the controller …

The following is NOT right.
[…]

What’s wrong with it? It looks OK to me.

Best,
Marnen


#15

ok like this…

<% form_tag do %>

<%= select(“location”, “location_id”, Location.all.collect {|l| [
l.name, l.id ]}, {:prompt => ‘Any Location’}) %>
<%= submit_tag “go” %>

<% end -%>


What about the controller …

The following is NOT right.

if params[:location_id] == "0" || params[:location_id] == nil # No 

Filter Selected - ID 0
@location = nil
@teachers = Teacher.all
else
@location = Location.find(params[:location_id])
@teachers = @location.teachers
end


#16

try to printout params …

def…
render :inline => params.inspect
end

or post your development.log record.

tom

bingo bob wrote:

its great, only one minor problem.

it doesn’t work.

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

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


#17

its great, only one minor problem.

it doesn’t work.


#18

form…

<% form_tag do %>

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

<% end -%>

With any location selected…I click go.

params

{“commit”=>“go”,
“authenticity_token”=>“nhPdYCMi1hx7joug0JECpiEqQFQWDfbh/z56Go5PTEE=”,
“action”=>“index”, “controller”=>“welcome”, “location”=>{“id”=>""}}

with a location selected…i click go.

params

{“commit”=>“go”,
“authenticity_token”=>“nhPdYCMi1hx7joug0JECpiEqQFQWDfbh/z56Go5PTEE=”,
“action”=>“index”, “controller”=>“welcome”, “location”=>{“id”=>“3”}}


so that looks like its ok at least location ID is getting set.


i am unsure what my controller should look like

I want to find all teachers if nothing is selected (default) and only
find the teachers for the location selected when that is the case. When
the filter is done the user should see the item they selected as well as
the filtered list.


#19

look:

bingo bob wrote:

params

{“commit”=>“go”,
“authenticity_token”=>“nhPdYCMi1hx7joug0JECpiEqQFQWDfbh/z56Go5PTEE=”,
“action”=>“index”, “controller”=>“welcome”, “location”=>{“id”=>“3”}}

params[:location][:id] is the way to get the location id

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

location is set

else

no location at all

end

find the teachers for the location selected when that is the case. When
the filter is done the user should see the item they selected as well as
the filtered list.

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

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


#20

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” %>

Find me in app/views/welcome/index.html.erb

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
]}, {:prompt => ‘Any Location’}) %>
<%= submit_tag “go” %>

<% end -%>

<%= render :inline => params.inspect %>


<% 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 %>

<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

======