"new" form as partial?

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

On Apr 2, 4:27 pm, Marian S. [email protected]
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 :slight_smile: 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

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

Craig

On 2 April 2010 22:27, Marian S. [email protected]
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/) 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