rails api doc
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
tom
bingo bob wrote:
rails api doc
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})
tom
bingo bob wrote:
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:
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.
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,
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
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,
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 -%>
bingo bob wrote: [...]
What about the controller ...
The following is NOT right.
[...]
What's wrong with it? It looks OK to me.
Best, Marnen
its great, only one minor problem.
it doesn't work.
try to printout params ..
def... render :inline => params.inspect end
or post your development.log record.
tom
bingo bob wrote:
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"}}
look:
bingo bob wrote:
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"}}
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
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
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?
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
> 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'
yup, expanding de morgan's laws in your head are a recipe for disaster. (and don't forget about the blank? method).
Fred
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.
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.
post your complete error code (with file and lines included). Then someone could help you.
tom
bingo bob 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…