Re: Sharing code between the edit and create actions

Eric Anderson
Thu, 27 Jan 2005 01:23:40 -0000

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


_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org