I''m trying to use collection_select, but it''s not working as I expect. The list populates correctly but the current value is not selected. @question contains a topic (via topic_id), and @topics is the list of all topics. Here''s my attempt to create a drop-down list of the topics. form("question") do | form | form << content_tag("b", "Topic") form << collection_select("topic", "topic_id", @topics, "id", "name") form << tag("br") + tag("br") end The select list displays all the proper topic names and id numbers, but the current value (@question.topic_id) is not selected. Hmm...does form("question") use the instance variable @question? If not, then I can see how topic_id would not be populated. Thanks for any help or suggestions. Jim -- Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm
Jim, Form helpers like text_field, collection_select etc currently work right only with instance variables. In your case topic is a local variable and therefore the current value is not passed correctly. I haven''t tested it but I think it might solve your problem if you''d add the following in your code:> > form("question") do | form |@topic = topic> form << content_tag("b", "Topic") > form << collection_select("topic", "topic_id", @topics, "id", > "name") > form << tag("br") + tag("br") > end > > The select list displays all the proper topic names and id numbers, > but the current value (@question.topic_id) is not selected. > > Hmm...does form("question") use the instance variable @question? If > not, then I can see how topic_id would not be populated. > > Thanks for any help or suggestions. > > Jim > -- > Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko,> Form helpers like text_field, collection_select etc currently work > right only with instance variables. In your case topic is a local > variable and therefore the current value is not passed correctly. I > haven''t tested it but I think it might solve your problem if you''d add > the following in your code:I''ll try your solution, but I''m confused because topic *is* an instance variable of @question. Oh, I see: topic needs to be an instance variable of the controller, not of the @question object.> >> form("question") do | form | > > @topic = topicI used @topic = @question.topic and that worked. Thank you.> >> form << content_tag("b", "Topic") >> form << collection_select("topic", "topic_id", @topics, "id", >>"name") >> form << tag("br") + tag("br") >> end >>Jim -- Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm