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

Bill Atkins batkins57 at gmail.com
Thu Jan 27 04:18:35 GMT 2005


Sounds good to me.  Thanks.

On Wed, 26 Jan 2005 20:23:40 -0500, Eric Anderson <eric at afaik.us> wrote:
> 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 at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> 
> 
> 
> 


-- 
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"


More information about the Rails mailing list