Single View For Create and List

Hunter Hillegas
Mon, 18 Jul 2005 01:27:34 -0000

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