Forum: Ruby on Rails including a blank in a dropdown

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bingo b. (Guest)
on 2009-05-09 17:02
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" %>


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

<hr>

<% if @teachers %>
  <% for teacher in @teachers %>
    <%= link_to [teacher.first_name + ' ' + teacher.last_name], teacher
%><br>
    <%= teacher.email %><br>
    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
Tom Z Meinlschmidt (Guest)
on 2009-05-09 18:20
(Received via mailing list)
rails api doc

   select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ]
}, {:prompt => 'Select Person'})


tom

bingo bob wrote:
> ---------
> <p>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
===============================================================================
bingo b. (Guest)
on 2009-05-09 18:50
Can't get that working unfortunately.
Tom Z Meinlschmidt (Guest)
on 2009-05-09 19:02
(Received via mailing list)
you have to use select() instead of select_tag()

or merge your options_from_collection_for_select with the "empty"
<option>please select</option>

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
===============================================================================
bingo b. (Guest)
on 2009-05-09 20:17
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.
Marnen L. (Guest)
on 2009-05-09 20:34
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
bingo b. (Guest)
on 2009-05-09 23:12
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.
Marnen L. (Guest)
on 2009-05-09 23:44
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
bingo b. (Guest)
on 2009-05-09 23:46
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.
bingo b. (Guest)
on 2009-05-10 16:18
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 -%>



<hr>

<% if @teachers %>
  <% for teacher in @teachers %>
    <%= link_to [teacher.first_name + ' ' + teacher.last_name], teacher
%><br>
    <%= teacher.email %><br>
    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
Marnen L. (Guest)
on 2009-05-10 17:48
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
bingo b. (Guest)
on 2009-05-10 18:10
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.
Marnen L. (Guest)
on 2009-05-10 18:20
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
bingo b. (Guest)
on 2009-05-10 18:32
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
Marnen L. (Guest)
on 2009-05-10 18:37
bingo bob wrote:
[...]
> What about the controller ...
>
> The following is NOT right.
[...]

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

Best,
Marnen
bingo b. (Guest)
on 2009-05-10 18:45
its great, only one minor problem.

it doesn't work.
Tom Z Meinlschmidt (Guest)
on 2009-05-10 19:30
(Received via mailing list)
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
===============================================================================
bingo b. (Guest)
on 2009-05-10 19:36
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.
Tom Z Meinlschmidt (Guest)
on 2009-05-10 19:49
(Received via mailing list)
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
===============================================================================
bingo b. (Guest)
on 2009-05-10 20:09
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


======
blasterpal (Guest)
on 2009-05-10 21:08
(Received via mailing list)
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 <removed_email_address@domain.invalid>
bingo b. (Guest)
on 2009-05-10 21:17
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
Julian L. (Guest)
on 2009-05-11 05:26
(Received via mailing list)
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
Ar C. (Guest)
on 2009-05-11 07:01
>     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'
Frederick C. (Guest)
on 2009-05-11 12:59
(Received via mailing list)
On May 11, 4:01 am, Ar Chron <removed_email_address@domain.invalid> 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
Ram (Guest)
on 2009-05-11 13:46
(Received via mailing list)
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. <removed_email_address@domain.invalid>
bingo b. (Guest)
on 2009-05-11 15:13
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.[]
Ram (Guest)
on 2009-05-11 15:37
(Received via mailing list)
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 <removed_email_address@domain.invalid>
Tom Z Meinlschmidt (Guest)
on 2009-05-11 16:27
(Received via mailing list)
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
===============================================================================
bingo b. (Guest)
on 2009-05-11 20:16
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:in
`send'
/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:in
`call_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:in
`perform_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:in
`realtime'
/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:in
`perform_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:in
`perform_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:in
`process_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:in
`process'
/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:in
`call'
Request

Parameters:

None
Show session dump

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}
Vinay S. (Guest)
on 2009-05-11 21:24
(Received via mailing list)
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
<removed_email_address@domain.invalid
> 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 !!!
bingo b. (Guest)
on 2009-05-11 22:37
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...
Vinay S. (Guest)
on 2009-05-12 10:22
(Received via mailing list)
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 <
removed_email_address@domain.invalid> wrote:

> Vinay S. wrote:
>
--
In Sport We Trust !!!
bingo b. (Guest)
on 2009-05-12 15:11
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" %>
<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
]}, {:include_blank => true}) %>
  <%= submit_tag "go" %>

<% end -%>

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

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

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.
Vinay S. (Guest)
on 2009-05-12 15:33
(Received via mailing list)
># 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? :)

 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
<removed_email_address@domain.invalid
> 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 !!!
This topic is locked and can not be replied to.