Re: Re: Sharing code between the edit and create actions

Jarkko Laine
Thu, 27 Jan 2005 18:02:15 -0000

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):

<%= @target == "edit" ? "Edit page" : "Add
new page" %>
<%= render_errors @page %>

<%= start_form_tag :action => @target, :action_prefix =>
"page" %>
<%= hidden_field "page", "id" %>


<label
for="page_name">Name
<%= text_field "page", "name",
"size" => 30 %>

Url (leave blank for
auto-generation)
<%= text_field "page", "url",
"size" => 30 %>

Content
<a href="#"
onClick="quickRedReference(); return
false;">(formatting)
<%= text_area "page", "content",
"rows" => 20, "cols" => 60
%>

Show on front page
<%= check_box "page", "show_p"
%>

<input type="submit" id="form_submit"
value="Save &#187;" />



<%= 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