# "new" form as partial?

**URL:** <https://www.ruby-forum.com/t/new-form-as-partial/186250>\
**Category:** Rails\
**Created:** [April 2, 2010, 11:29pm UTC](https://www.ruby-forum.com/t/new-form-as-partial/186250 "2010-04-02T23:29:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 2, 2010, 11:29pm UTC](https://www.ruby-forum.com/t/new-form-as-partial/186250/1 "2010-04-02T23:29:50Z")

</div>

Hi!

I think I’m trying to accomplish something very common, but I can’t  
find a way to do it.

Creating a certain type of database record is a very frequent task in  
my application (or at least it should be ;-)). So I want to be able to  
place the form for creating that database record in practically any  
view, not just the /mymodel/new view.

I figured that this might be done using a partial, but since I’m  
pretty new to rails, I don’t get it one.

Can anybody point me to a tutorial for this problem?

I’m using rails 2.3.5.

Thank you!  
Marian

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 1:56am UTC](https://www.ruby-forum.com/t/new-form-as-partial/186250/2 "2010-04-03T01:56:10Z")

</div>

On Apr 2, 4:27 pm, Marian S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Creating a certain type of database record is a very frequent task in  
> my application (or at least it should be ;-)). So I want to be able to  
> place the form for creating that database record in practically any  
> view, not just the /mymodel/new view.

I’m sure you can refactor code to fit almost anything into a single  
new/edit partial 🙂 But in my small experience the models are  
different enough so that having different views for different models  
makes the code more readable. That said, I almost always share new  
and edit for a model in a partial:

e.g. new.html.erb  
\<%= render( :partial =\> “form”, :locals =\> { :buttonname =\> ‘Add’ } )  
%\>

edit.html.erb  
\<%= render( :partial =\> “form”, :locals =\> { :buttonname =\>  
‘Update’ } ) %\>

\_form.html.erb  
\<%= error\_message\_on :lab, :base %\>  
\<% form\_for …, do |f| %\>  
\<%= f.submit buttonname %\>  
…  
\<% end %\>

Ryan B. has great screencasts on this sort of thing, and I found  
his Mastering Rails Forms

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

($5 a video) to be a great start.

Craig

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 9:28am UTC](https://www.ruby-forum.com/t/new-form-as-partial/186250/3 "2010-04-03T09:28:32Z")

</div>

On 2 April 2010 22:27, Marian S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I figured that this might be done using a partial, but since I’m  
> pretty new to rails, I don’t get it one.

Assuming you know the basics of rendering a partial within a view (if  
not look at the guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)) then all you  
have to do is include a path in the render partial to tell it where it  
is.  
render :partial =\> “/somefolder/form”  
will look for views/somefolder/\_form.html.erb

Colin
