# Two forms for two models - one page

**URL:** <https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898>\
**Category:** Rails\
**Created:** [April 15, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898 "2006-04-15T21:26:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 15, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/1 "2006-04-15T21:26:38Z")

</div>

I’ve got two forms for two different models on the same page. What I  
want is for one of the forms to return you to the same “new” page and  
not clear the data in the other form. I was hoping it would be as  
simple as render :action =\> ‘new’ but no such luck.

Any thoughts?

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 16, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/2 "2006-04-16T21:07:45Z")

</div>

Anyone? 🙂

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 17, 2006, 1:12am UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/3 "2006-04-17T01:12:55Z")

</div>

Ok, the problem is that, when you submit one form, the application does  
not get the data from the other form, so it cannot store it (in the  
session).

I see 2.5 possibilities:

#1: Unify the forms  
#2.1: Use JavaScript to duplicate the entered text into hidden fields,  
so form1 will submit the data from form1 and form2 and vice versa  
#2.2: Use AJAX to periodically update the contents of the forms and save  
them in the session.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 17, 2006, 1:46am UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/4 "2006-04-17T01:46:49Z")

</div>

Adam B. wrote:

> I’ve got two forms for two different models on the same page. What I  
> want is for one of the forms to return you to the same “new” page and  
> not clear the data in the other form. I was hoping it would be as  
> simple as render :action =\> ‘new’ but no such luck.
> 
> Any thoughts?
> 
> Thanks,
> 
> Adam

Really, just stick the forms together. If, say, model A’s “form” is not  
changed, then it won’t affect the database. It will be way easier than  
trying to use JS/AJAX or anything like that.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 17, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/5 "2006-04-17T21:59:26Z")

</div>

Ah! That makes sense. Thanks.

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 17, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/6 "2006-04-17T22:18:59Z")

</div>

This works, but I can’t help but feel that it should be simpler…

def create

#this seems to be the only way to test whether a tag needs to be created  
unless (params[:tag][:name]) == “”  
@tag = Tag.new(params[:tag])  
@tag.save

#without this line the tag field will still have text in it.  
@tag = Tag.new

#and without these I get an error, because render :action doesn’t run  
the controller action. Right?  
@show = Show.new(params[:show])  
@show.tags = Tag.find(params[:tag\_ids]) if (params[:tag\_ids])  
@tags = Tag.find\_all  
render :action =\> ‘new’  
else  
@show = Show.new(params[:show])  
@show.tags = Tag.find(params[:tag\_ids]) if (params[:tag\_ids])  
@show.save  
@show.user = session[:user]  
if @show.save  
session[:user].shows \<\< @show  
flash[:notice] = ‘Show was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
@show.destroy  
@tags = Tag.find\_all  
render :action =\> ‘new’  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 17, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/two-forms-for-two-models-one-page/57898/7 "2006-04-17T22:21:34Z")

</div>

Oops, this is wrong:

> #and without these I get an error, because render :action doesn’t run  
> the controller action. Right?

Without the @tags assignment I get an error, because the page for “new”  
calls @tags. Without the @show assignments I just get empty data fields.

> ```
> @show = Show.new(params[:show])
> @show.tags = Tag.find(params[:tag_ids]) if (params[:tag_ids])
> @tags = Tag.find_all
> render :action => 'new'
> else ...
> 
> ```

-Adam
