rails newbie : routing error

After much ado about the naming of model adn controllers...

model : Expense controller : Expenses route : map.resources :expenses

Here I am using mysql database and created a unique index, since db:migrated created an id object and made it as primary key. Now in the def create    @expense = Expense.new(params[:expense])    if @expense.save      flash[:viola] = 'New expense saved.. be frugal, save during this bad economy'      redirect_to expenses_path    else      render :action => 'new'      #redirect_to new_expense_path    end

Here I try to save, and if any error, render action =>'new'. When I do that I get the

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Extracted source (around line #1):

1: <% form_for @e1 do |er| %> 2: <p> 3: name :<br /> 4: <%= er.text_field :name %>

I have the @e1 defined correctly in the controller (how can it work when I go to the new page directly using localhost:300/expenses/new ).

And the trace : vendor/rails/actionpack/lib/action_controller/record_identifier.rb: 72:in `dom_id' vendor/rails/actionpack/lib/action_view/helpers/ record_identification_helper.rb:16:in `dom_id' vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:264:in `apply_form_for_options!' vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:248:in `form_for' app/views/expenses/new.html.erb:1:in `_run_erb_47app47views47expenses47new46html46erb' app/controllers/expenses_controller.rb:18:in `create'

What I don't understand is the form_for works fine when used initially, or even through the redirect_to new_expense_path but not through the action => 'new'

Any help is appreciated......

Replace @expense = Expense.new(params [: expense]) at @e1 = Expense.new(params [: expense]) in the create action since the render =>: new variable @e1 is not defined, but in view, it is used.

That worked, care to explain the theory behind it, or some links how this routing works???

It's not routing.it's - MVC. Then your render 'new' template, view use instance variable defined in the current action... In the view you difened @e1 variable, but it's not defined in the create action.

Thanks guys… worked …