سؤال

I'm really stuck in a issue.

I've a RoR application in which I'd like to populate select_tag with filtered options lets say Physicians. Here is my select_tag in view:

<%= select_tag "phyID",options_from_collection_for_select(@physicians,'id','fullNamePhy'),:include_blank => true %>

In controller I have

@physicians=User.find_by_userType('Physician')

but I'm getting error:

undefined method `map' for #<User:0x3b09820>

It seems like I have to use User.all instead of User.find. Please let me know any work around. Thanks in advance

هل كانت مفيدة؟

المحلول

This should work:

@physicians = User.where(userType: 'Physician')

You're getting error because options_from_collection_for_select expects object that behaves like Array, for example ActiveRecord::Relation instance. But find_by_* dynamic finder returns object representing singular record, User instance in this case.

BTW, column names in Rails are by convention named with underscore instead of camel case , like user_type.

نصائح أخرى

You can use dynamic find_all_by finder

@physicians = User.find_all_by_userType('Physician')

find_by_column_names returns single record. Where as select_tag expects collection/array of record

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top