Re: Single View For Create and List

Deirdre Saoirse Moen
Mon, 18 Jul 2005 03:34:19 -0000

On Jul 17, 2005, at 6:27 PM, Hunter Hillegas wrote:

> I am converting a Java app to Rails.
>
> In the Java app, there is a single page to:
>
> 1. Show DB contents in a list.
> 2. Create new DB items.

Hmm, modeled after one of the WebObjects examples perhaps? :)

Here''s what I''ve got for a controller that does that stuff. It
seems
to work thus far.

class EditorRepliesController < ApplicationController
   def index
     list
     render_action ''list''
   end

     def list
         @pageTitle = "List Editor Replies"
         @editor_reply_pages, @editor_replies =
paginate :editor_reply, :per_page => 10
     end

   def create
     @editor_reply = EditorReply.new(@params[:editor_reply])
     if @editor_reply.save
       flash[''notice''] = ''EditorReply was
successfully created.''
       redirect_to :action => ''list''
     else
       flash[''notice''] = ''EditorReply could not be
created.''
       redirect_to :action => ''list''
     end
   end

   def update
     @editor_reply = EditorReply.find(@params[:id])
     if @editor_reply.update_attributes(@params[:editor_reply])
       flash[''notice''] = ''EditorReply was
successfully updated.''
       redirect_to :action => ''list''
     else
        flash[''notice''] = ''EditorReply could not be
updated.''
       redirect_to :action => ''list''
    end
   end

end


List rhtml:





Editorial Replies





             <th align="left"
class="storytitle">Reply
Description

         <% @editor_replies.each do |@editor_reply| %>
<%= link_to ''Edit'',
:action => ''list'', :id =>
@editor_reply %>
<%= @editor_reply.respName %>

         <% end %>



     <% if @params[:id] == nil %>
         <%= render_partial "new" %>
     <% else %>
         <%= render_partial "edit" %>
     <% end %>