# Single form w/ relationships: how do I integrate it?

**URL:** <https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768>\
**Category:** Rails\
**Created:** [March 14, 2006, 2:56am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768 "2006-03-14T02:56:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 2:56am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/1 "2006-03-14T02:56:19Z")

</div>

OK - I know this has been asked before, but I cannot find it in the  
archives. Forgive me if I’m creating more noise than I should.

I want to know the easiest way to deal with this scenario:

I have articles. I have categories for the articles.

On the form where you write an article, there needs to be a free-form  
field to entire the category.

The create() method will build the article, find\_or\_create\_by\_name the  
category, and assign it to the article. After that, the article is  
saved.

I need to display an error if the category isn’t included, and I need  
it to act like a normal form error: add its message to the list of  
errors, and highlight the field.

Does anyone know an easy way to do this? I know it’s right here in  
front of me…

Thanks in advance,

Michael

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 3:17am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/2 "2006-03-14T03:17:43Z")

</div>

Hello Michael,

2006/3/13, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I have articles. I have categories for the articles.

See an older blog entry of mine:

[http://blog.teksol.info/articles/2006/01/03/belongs\_to-user-interface-take-ii](http://blog.teksol.info/articles/2006/01/03/belongs_to-user-interface-take-ii)

Hope that helps !

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 3:35am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/3 "2006-03-14T03:35:52Z")

</div>

That’s a great article, and I have used that technique before.

My question is this: how do I do the same, but with a ‘text\_field’? I  
need to allow the user to enter in whatever he or she likes, but it  
must be something.

Any ideas how to accomplish this?

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 3:44am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/4 "2006-03-14T03:44:57Z")

</div>

2006/3/13, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> That’s a great article, and I have used that technique before.
> 
> My question is this: how do I do the same, but with a ‘text\_field’? I  
> need to allow the user to enter in whatever he or she likes, but it  
> must be something.
> 
> Any ideas how to accomplish this?

Oh, sorry. I hadn’t read your message properly.

You probably want to use a text\_field\_with\_auto\_complete:

[http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000377](http://api.rubyonrails.com/classes/ActionView/Helpers/JavaScriptMacrosHelper.html#M000377)

Hope that helps !

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 4:40am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/5 "2006-03-14T04:40:26Z")

</div>

I’d go one further - I’d like the user to autocomplete, but to also be  
allowed to invent a new entry for ‘color’ at the same time as entering a  
new product …

How would that work?

-jim

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 4:43am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/6 "2006-03-14T04:43:37Z")

</div>

I’m in the same spot, Jim.

I’m thinking that using the ‘text\_field\_with\_autocomplete’ coupled  
with a ‘find\_or\_create\_by\_name’ combination would work.

Haven’t tried it yet, though.

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/7 "2006-03-14T12:54:31Z")

</div>

Thanks, Peter.

I’ll give this a shot today.

---

<div class="post-metadata">

**Author:** ![mgreenly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85f322/32.png) [@mgreenly](https://www.ruby-forum.com/u/mgreenly)\
**Post date:** [March 14, 2006, 8:11am UTC](https://www.ruby-forum.com/t/single-form-w-relationships-how-do-i-integrate-it/54768/8 "2006-03-14T08:11:38Z")

</div>

I’ve done something I think is similar so I’ll take a stab. I haven’t  
used find\_or\_create\_by so there might be a better way if I knew what  
it returns in the case of errors. Seems like the way i used  
find\_or\_create\_by below makes the whole thing bulky. Anyway this might  
spark an idea.

The line below

if @category.name.errors.on(:name)

is very weak maybe you can write

if @category.name.errors.length

or something better

> I need to display an error if the category isn’t included, and I need  
> it to act like a normal form error: add its message to the list of  
> errors, and highlight the field.

In the models

class Category \< ActiveRecord::Base  
validates\_presence\_of :name  
has\_many :articles  
end

class Article \< ActiveRecord::Base  
validates\_presence\_of :the\_article\_text  
belongs\_to :category  
end

In the controller action

def new\_article

if params[:submit]  
@category=Category.find\_or\_create\_by\_name(params[:category])  
@article=Article.new(params[:article])  
if @category.name.errors.on(:name)  
render  
return  
else  
@article.category\_id = @category.id  
unless @article.save  
render  
return  
end

```
   redirect_to :action=>"somewhere"
   return
end

```

end

@category = Category.new  
@article = Article.new  
end

In new new\_article.rhtml view

\<%= form\_tag %\>  
\<%= text\_field :category, :name %\>  
\<%= text\_area :article, :the\_article\_text %\>  
\<%= submit\_tag %\>  
\<%= end\_form\_tag %\>
