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. The page has a list on the left and a form on the right. The list shows existing DB contents as hyperlinks. When you navigate to this page at first, the form on the right is used to create new items. I started off with scaffolded code as an example. I distilled the views into a single file, index.rhtml. I got the page to list and to allow me to create items, as long as validation succeeds. The problem is displaying errors when validation fails. Currently, the controller looks like this: def index @user_account_pages, @user_accounts = paginate :user_account, :per_page => 20 end def create @new_user_account = UserAccount.new(params[:new_user_account]) if @new_user_account.save flash[:note] = ''You have successfully added the user.'' redirect_to :action => ''index'' else render :action => ''index'' end end On create, if the validation fails, the method proceeds to call ''render :action''. This fails with a nil object. My understanding is that render :action is simply rendering the view. Of course this fails since the index method in the controller was never called and it sets up the list of items to display in that view. If I change render to redirect_to, I seem to lose my validation information. So, any suggestions to a newbie as to how I should go about doing this? Thanks, Hunter
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: <div id="content"> <div class="post"> <h2 id="editstuff">Editorial Replies</h2> <table width="100%"> <col width="5%"> <col width="95%"> <tr> <th> </td> <th align="left" class="storytitle"><em>Reply Description</em></td> </tr> <% @editor_replies.each do |@editor_reply| %> <tr><td><%= link_to ''Edit'', :action => ''list'', :id => @editor_reply %></td> <td><%= @editor_reply.respName %></td> </tr> <% end %> </table> </div> <% if @params[:id] == nil %> <%= render_partial "new" %> <% else %> <%= render_partial "edit" %> <% end %> </div>