0

In my view I have a collection_select. I want it to prompt the user to select if the session variable is set to 0. The else works fine but if the condition is 0 then it is telling me that there is no method 'id'.

- if session[:ptype_id] == 0
  = collection_select :ptype, :id, Ptype.all.order(:part_type), :id, :part_type, {:prompt => "Select Type"}, :onchange => "ptype_set(value)"
- else
  = collection_select :ptype, :id, Ptype.all.order(:part_type), :id, :part_type, {:selected => session[:ptype_id]}, :onchange => "ptype_set(value)"

The error...

undefined method `id' for #<Ptype::ActiveRecord_Relation:0x007f9bcf138428>
Beengie
  • 1,588
  • 4
  • 18
  • 36
  • Let's see your `ptype_set()` function. – JTG Sep 29 '14 at 16:48
  • or better yet: take it out, see if it works. Then if it does, show us your `ptype_set()` function – JTG Sep 29 '14 at 16:51
  • It did not work without the type_set() – Beengie Sep 29 '14 at 17:37
  • Well, unfortunately, I cannot replicate your issue. The error suggests that you are doing something like `Ptype.all.id` which won't work because there is no `id` to a collection of results, but the syntax you've provided should work. Compounded with the fact that it *does* work when you use the exact same syntax in the `else` statement. When does the error pop up? When you are trying to render the page, or when you're trying to submit? – JTG Sep 29 '14 at 17:50
  • it happens when I submit a zero value for the `session[:type_id]`. I am trying to make it so there is a "Select Type" when it passes a zero along with showing all records. When a user selects a type, it calls the javascript, sets the session variable, and refreshes the page but filters to the type. Thanks for helping me out on this BTW. – Beengie Sep 29 '14 at 18:55

0 Answers0