Hi,
I have a model called Plan with fields plan_id,Title,description. I have
another model called Buy with fields title,description,Plan. I would
like to create a “New” View for Buy where we have a dropdown listing all
the Titles from Mediaplan. All the Plan:titles are unique and should
come from database.
I created a migration to create a Buy with a foreign key constraint to
Plan.
create_table :buys do |t|
t.integer :plan_id, :null=>false, :options =>
"CONSTRAINT fk_buys_plans REFERENCES plans(id)"
t.string :title, :null=>false
t.text :description, :null=>false
t.timestamps
end
How do I create a dropdown list in a Buy’s View with all the Plan titles
?
Thanks!
use collection_select tag for this
collection_select(:buy, :plan_id, Plan.find(:all), :id, :title, {:prompt
=> true})
Thanks
Brijesh S.
Brijesh S. wrote:
use collection_select tag for this
collection_select(:buy, :plan_id, Plan.find(:all), :id, :title, {:prompt
=> true})
Thanks
Brijesh S.
Thanks for responding. I tried that, but it was not displayed as
dropdown. Instead it showed up as plain text on the browser
collection_select(:mediabuy, :mediaplan_id, Mediaplan.find(:all), :id,
:title, {:prompt=> true})
Thanks!
Brijesh S. wrote:
use collection_select tag for this
collection_select(:buy, :plan_id, Plan.find(:all), :id, :title, {:prompt
=> true})
Thanks
Brijesh S.
Thanks! It works now. I made a dumb mistake by not putting that
collection_select in <%=%>.
Lady H. wrote:
Thanks! It works now. I made a dumb mistake by not putting that
collection_select in <%=%>.
+1 if you take that DB access out of your view…
In the controller, put in a :
@plans = Plan.find(:all)
and swap the view code to:
<%= collection_select(:buy, :plan_id, @plans, :id, :title, {:prompt =>
true}) %>
to separate data acquisition from presentation.