[Rails] Re: Sharing code between the edit and create actions

Eric Anderson eric at afaik.us
Thu Jan 27 01:23:40 GMT 2005


Bill Atkins wrote:
> Is there an easy way to reuse the code used to generate an edit form
> to generate the form for the create action?  In the ONLamp tutorial, a
> custom edit page is written, but the changes have no effect on
> creating a recipe.

Why not just render the edit template in the create action. I haven't 
read the article so I don't know the exact models but something like 
this works great for me in my projects.

RecipeController < ActionController::Base

def new
	@rec = Recipe.new
	render_action 'edit'
end

def edit
	@rec = Recipe.find @params['id']
end

end

Then your edit.rhtml file will simply read from the @rec object. It will 
be either blank or filled in with all the details of an existing object.

Eric
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://one.textdrive.com/pipermail/rails/attachments/20050126/e9a6bc1c/signature.bin


More information about the Rails mailing list