Related <select> Dropdowns in Rails Views
This is a pretty common scenario (I would think). You have a nested model that you want to be able to select the item that the current item depends on (the client for a project, the manufacturer for an appliance). Using form_for, you can do something like this.
Chat with JTJ:
What you do is create a select tag with the “
select” method like so: (using the client/project scenario)
f.select(:client_id,The next value you pass to the select method has to be an array of text/value pairs
like this:
[ [ "Jason Johnson", 1], ["Trey Piepmeier", 2], ["Royall", 3] ]In order to create this array, you do a find on your clients table and use Ruby’s “
collect” method.like so:
Client.find(:all).collect {|c| [ c.name, c.id ]}So, altogether now:
f.select(:client_id, Client.find(:all).collect {|c| [ c.name, c.id ] })
Other sources:
Title Case in ERb
Use .titleize or .titlecase. I’m using this in the <title> tag of my application.rhtml:
<%= controller.action_name.titleize %>