#57 Create Model Through Text Field

Let's say you provide a select menu for setting which category a given product belongs to, but you also want the option of creating a new category by typing the name in a text field. See a great way to do that in this episode.

@ron, actually the controller doesn't know anything about the Category model - it only creates the Product model. The association is set by using the "create_category" method. This is provided by the belongs_to association and automatically sets the category to that product when it is created.

How about the other way around. When I create a category I would like to create a couple of products belonging to that category as well. I've realised there is something like category.products.create but I dont know how to use it. Any pointers?

@JT, I wouldn't use this technique if you have to fill in more than one attribute for the related model. That can get pretty messy in the code. Instead, fields_for seems like a better solution that way you can group it in a second hash and not have to create a virtual attribute for every related attribute. Maybe I'll make a screencast on this in the future.

@Oskar, this is on my todo list for a future episode. :)

@Ioribox, That "f" variable is a form builder which was created through form_for. I'm just passing it as a local variable to the partial which is why you don't see the form_for statement.

Editing a product doesn't seem to allow the creation of a new category, only creating a new product. I followed the implementation given in the screencast fairly closely, so do you think you could offer a guess as to what might be going wrong? Cheers.

I noticed that using the create_category method doesn't run the validations in the related model "category.rb" Is there a good way around this or do we need to redo our validation checks in the method like LoRiBoX's comment above. I'm sure there is a DRYer way to do it but I'm not sure how.

Hi Ryan. First of all, thanks for the excellent Railcast-episodes! They really help finding my way through.
I'm finding the title of this episode misleading, but maybe I don't understand things well enough just yet. Anyhow, imho we're not creating models but records, so I'd call the episode "Create record through text field".
cheers from the nitpicking-department

Ryan, what about some validation examples? For example, I'm validating for :company_id given one of the companies from the drop-down are selected, however, I don't want to run the validation if new_company_name is present. This doesn't seem to work:

Has anyone did this using an auto complete instead of a select and text box. I was hoping to add the feature where the user would start typing a name, if it was in the list it would add it if not it would create a new one.

I'm using rails 2.0.2. The params hash *does* have the value that I'm passing through the form, however the attr_accessor that I created is *not* getting populated. I am having to explicitly set the value of attr_accessor from the params hash.

Ryan thanks for shedding some rails light to my noob darkness. I have a problem with :category_id. I get the "undefined method" error. I read the collection_select api for rails 3 and it doesn't seem I'm doint anything wrong. Yet I still get the error message. I'm using this:

<%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => true%>
I just want to add a category to each product but I get this annoying error.

PS: Category and Product are both generated with scaffold.

Thank you again for your time and the great Rails screencasts. Kudos man.

I ran in to the following issue
with <%= f.collection_select :category_id, Category.find(:all), :id, :name, :prompt => "Select a Category" %>
we let user not to choose category_id, but use can also not type into create category field - so we let user to create product without category
if i add validation on cateory_id it did not save