Forum: Ruby on Rails Rails form error message oddness...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James W. (Guest)
on 2006-04-10 16:16
To get error messages for my form to show up I have to repeat myself in
the controller so that the render of the view including error messages
show up.

-------------------------
My Controller:
-------------------------

class ClientsController < ApplicationController

#---- index --------------------------------
  def index
    list
    render :action => 'list'
  end

#---- list --------------------------------
  def list
   @client_pages, @clients = paginate(:clients, :order_by => 'name')
  end

#---- new client --------------------------------
  def new
  #raise params.inspect
    @client = Client.new(@params[:client])

    if request.post? && @client.save
      flash['notice'] = 'Client created successfully'
      redirect_to :action => 'list'
    else
      @client_pages, @clients = paginate(:clients, :order_by => 'name')
      render :action => 'list'
    end
  end

end




I am having to repeat the line: @client_pages, @clients =
paginate(:clients, :order_by => 'name') just before the render :action
=> 'list'. Surely rendering the action would keep all of the variables
like @clients.

Is this the correct way to achieve this or am I making things really
complicated? Thanks for your help.

My view is listed here to help:

-----------------------------------
My View:
-----------------------------------

<div id="newClient">

<%= form_tag :action => 'new' %>

    <div class="basicForm">
     <h3>Add a new Client</h3>
    <%= error_messages_for 'client' %>

      <dl>
      <dt><label for="client_name">Client Name:</label></dt>
      <dd><%= text_field "client", "name", :size => 40 %></dd>

      <dt><label for="client_reference">Client Reference:</label></dt>
      <dd><%= text_field "client", "reference", :size => 40 %></dd>
      </dl>

<%= submit_tag "post" %>

    </div>

  <%= end_form_tag %>

  <div id="clientList">

    <h3>Current Clients </h3>
    <p>A list of the current active clients.</p>

    <%= start_form_tag :action=> "new" %>
      <% for client in @clients -%>
        <%= render(:partial => "client_list", :object => client) %>
      <% end -%>

    <%= end_form_tag %>

    <div id="pageNavigation" class="clear">
      <%= pagination_links(@client_pages) %>
    </div>

</div>
This topic is locked and can not be replied to.