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.
Bill
--
$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"
Francisco Hernandez
2005-Jan-27 00:56 UTC
Re: Sharing code between the edit and create actions
I used Partials for this: http://rails.rubyonrails.org/classes/ActionView/Partials.html I have a _form.rhtml partial that gets used with edit/create actions 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. > > Bill >
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 http://lists.rubyonrails.org/mailman/listinfo/rails
Bill Atkins
2005-Jan-27 04:18 UTC
Re: Re: Sharing code between the edit and create actions
Sounds good to me. Thanks. On Wed, 26 Jan 2005 20:23:40 -0500, Eric Anderson <eric-ANzg6odk14w@public.gmane.org> 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-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.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"
Shouldn''t the edit form target the ''update'' action and the new form target ''create''? Unless you''re doing something creative like a unified action that checks for object id to be non-null and then creates/updates OR saving @rec first, I can''t see how this would work. Also, the edit form probably needs a hidden field for ID and the create form does not. I usually use a _form.rhtml partial but only include the body of the form, that way I can have different form targets in edit and new and add the hidden id field to edit. Brian On Wed, 26 Jan 2005 20:23:40 -0500, Eric Anderson <eric-ANzg6odk14w@public.gmane.org> 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-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Jarkko Laine
2005-Jan-27 18:02 UTC
Re: Re: Sharing code between the edit and create actions
Brian,
You can perfectly well use the same view file in both "edit" and
"new"
actions. Here''s an example of that (note the target variable which is
different in the two methods):
def add_page
case @request.method
when :get
@page = Page.new
@target = "add"
render_action "edit_page"
when :post
@params["page"]["content_html"] =
HtmlEngine.transform(@params["page"]["content"])
@params["page"]["url"] =
TextHelp.sanitize(@params["page"]["name"]) \
unless @params["page"]["url"].size > 0
@page = Page.new(@params["page"])
if @page.valid?
Page.transaction do
raise "rollback" unless @page.save
flash["notice"] = ''Sivu luotiin
onnistuneesti.''
expire_action :action => "index"
redirect_to :controller => "admin"
end
end
end
end
def edit_page
case @request.method
when :get
@page = Page.find(@params["id"])
@target = "edit"
render_action "edit_page"
when :post
@page = Page.find(@params["page"]["id"])
# save the content also in HTML to avoid conversion in page
view phase
@params["page"]["content_html"] =
HtmlEngine.transform(@params["page"]["content"])
@params["page"]["url"] =
TextHelp.sanitize(@params["page"]["name"]) \
unless @params["page"]["url"].size > 0
# edit the attributes
@page.attributes = @params["page"]
if @page.valid?
Page.transaction do
raise "rollback" unless @page.save
flash["notice"] = ''Sivu päivitettiin
onnistuneesti.''
expire_action :action => "index"
redirect_to :controller => "admin"
end
end
end
end
###################
and here comes the relevant part of edit_page.rhtml (note that the
action of the form is set by the target instance variable):
<h1><%= @target == "edit" ? "Edit page" : "Add
new page" %></h1>
<%= render_errors @page %>
<%= start_form_tag :action => @target, :action_prefix =>
"page" %>
<%= hidden_field "page", "id" %>
<dl>
<dt><label
for="page_name">Name</label></dt>
<dd><%= text_field "page", "name",
"size" => 30 %></dd>
<dt><label for="page_name">Url (leave blank for
auto-generation)</label></dt>
<dd><%= text_field "page", "url",
"size" => 30 %></dd>
<dt><label for="page_content">Content
<small><a href="#"
onClick="quickRedReference(); return
false;">(formatting)</a></small></label></dt>
<dd><%= text_area "page", "content",
"rows" => 20, "cols" => 60
%></dd>
<dt>Show on front page</dt>
<dd><%= check_box "page", "show_p"
%></dd>
<dt><input type="submit" id="form_submit"
value="Save »" /></dt>
</dl>
<%= end_form_tag %>
HTH,
Jarkko
On 27.1.2005, at 18:49, Brian L. wrote:
> Shouldn''t the edit form target the ''update''
action and the new form
> target ''create''? Unless you''re doing something
creative like a unified
> action that checks for object id to be non-null and then
> creates/updates OR saving @rec first, I can''t see how this would
work.
>
> Also, the edit form probably needs a hidden field for ID and the
> create form does not.
>
> I usually use a _form.rhtml partial but only include the body of the
> form, that way I can have different form targets in edit and new and
> add the hidden id field to edit.
>
> Brian
>
>
>
> On Wed, 26 Jan 2005 20:23:40 -0500, Eric Anderson
<eric-ANzg6odk14w@public.gmane.org>
> 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-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>>
>
>
> --
> The years ahead pick up their dark bags.
> They move closer. There''s a slight rise in the silence
>
> then nothing.
> --
> (If you''re receiving this in response to mail sent to
> bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be
concerned This is my new address,
> but mail will be forwarded here indefinitely)
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Sarah Wedde
2005-Jan-27 19:30 UTC
Re: Re: Sharing code between the edit and create actions
This is what I''ve been doing--I think it came from the original
tutorial on
the wiki.
def new
@category = Category.new
@target = "create"
render_action "edit"
end
def edit
@category = Category.find(@params["id"])
@target = "update"
end
and then in the edit form
<%= form_tag({:action => @target}) %>
Sarah
On 28/1/05 5:49 AM, "Brian L."
<zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> Shouldn''t the edit form target the ''update''
action and the new form
> target ''create''? Unless you''re doing something
creative like a unified
> action that checks for object id to be non-null and then
> creates/updates OR saving @rec first, I can''t see how this would
work.
>
> Also, the edit form probably needs a hidden field for ID and the
> create form does not.
>
> I usually use a _form.rhtml partial but only include the body of the
> form, that way I can have different form targets in edit and new and
> add the hidden id field to edit.
>
> Brian