can not redirect to nil

I am using Ruby 2.0.0 and Rails 3.2.16.

I get following error after clicking on "Add to count" button :

ActionController::ActionControllerError (Cannot redirect to nil!):

app/controllers/line_items_controller.rb:53:in `block (2 levels) in
create'

app/controllers/line_items_controller.rb:51:in `create'

I attached code of line_items_controller.rb and full trace of error.
please find it.

Error occur at following line as display in error:

format.html { redirect_to @line_item.expensescounter } #line No:53

I attached full trace of error. please find it.

Any help would be appreciated.

Kind regards.

Attachments:
http://www.ruby-forum.com/attachment/9951/full_trace_of_error.odt

Sometimes error messages can be meaningful. The error says Cannot
redirect to nil, so it may be that
@line_item.expensescounter is nil.

Simple debugging can be performed using Logger.info, which inserts
into the log file (eg log/development.log), so you can confirm this by
inserting, before the problem line
Logger.info( "expensescounter is: #{@line_item.expensescounter}"

If it is nil, if you cannot see the problem, you can put additional
logs in to home in on the problem.
Also in the log file you will find useful information showing the
parameters being passed into the action.

Colin

Simple debugging can be performed using Logger.info, which inserts
into the log file (eg log/development.log), so you can confirm this by
inserting, before the problem line
Logger.info( "expensescounter is: #{@line_item.expensescounter}"

I used logger.info for debugging and it insert in development.log file
that @line_item.expensescounter is nil.

If it is nil, if you cannot see the problem, you can put additional
logs in to home in on the problem.
Also in the log file you will find useful information showing the
parameters being passed into the action.

I have attached development.log file(productionlog.odt) in my question.

How could i solve this error?

Kind regards.

By working out why it is nil. It is called debugging. Have a look
where you setup that value and if necessary put more logging in till
you see where the problem is.

Colin

By working out why it is nil. It is called debugging. Have a look
where you setup that value and if necessary put more logging in till
you see where the problem is.

I will find out why it is nil.
Thank you for your help.

Kind regards.

I have setup expensescounter in application_controller.rb file like as
follow:

class ApplicationController < ActionController::Base

  protect_from_forgery
  include SessionsHelper

  private
    def current_expensescounter
      Expensescounter.find(session[:expensescounter_id])
       rescue ActiveRecord::RecordNotFound
       expensescounter = Expensescounter.create
       session[:expensescounter_id] = expensescounter.id
       expensescounter
    end
end

I can not able to find solution of this error.

Any help would be appreciate.

Kind regards.

As was suggested before, use logging to see what is actually happening. When you're starting without a clue, log after every line to see where things become different than what you expect.

That code is just returning an object. It is not allocating a value
to @line_item.expensescounter.

Colin

As was suggested before, use logging to see what is actually happening.
When you're starting without a clue, log after every line to see where
things become different than what you expect.

I used logging to see what is actually happening. I found
expensescounter_id comes nil which is not my requirement.

expensescounter_id should have some value. Could you help me in this?

That code is just returning an object. It is not allocating a value
to @line_item.expensescounter.

Yes. When i click on "Add to count" button, i got expensescounter_id
comes nil. BUT it should have some value.

How could i do this?

You have not shown us the code where you set expensescounter_id, or
the code that you expect that value to be set. You have shown us the
code where expenses_counter.id would be setup but presumably
expensescounter_id is an attribute of a different object.

Colin

You have not shown us the code where you set expensescounter_id, or
the code that you expect that value to be set. You have shown us the
code where expenses_counter.id would be setup but presumably
expensescounter_id is an attribute of a different object.

I following "Agile web development with rails 3.2" book.

Yes, expensescounter_id is an attribute.

I use that attribute in line_item.rb file like as follow:

class LineItem < ActiveRecord::Base
   attr_accessible :quantity_id, :expensescounter_id

  belongs_to :quantity
  belongs_to :expensescounter
end

You have not shown us the code where you set expensescounter_id, or
the code that you expect that value to be set. You have shown us the
code where expenses_counter.id would be setup but presumably
expensescounter_id is an attribute of a different object.

I following "Agile web development with rails 3.2" book.

Yes, expensescounter_id is an attribute.

I use that attribute in line_item.rb file like as follow:

class LineItem < ActiveRecord::Base
   attr_accessible :quantity_id, :expensescounter_id

  belongs_to :quantity
  belongs_to :expensescounter
end

expensescounter_id should be set when you add the expensescounter to
the lineitem. That is the code you need to debug, or show us.

Colin

expensescounter_id should be set when you add the expensescounter to
the lineitem. That is the code you need to debug, or show us.

How could i debug that code? I use logger like
"logger.debug "expensescounter_id is #{params[:expensescounter_id]}""
AND
expensescounter_id comes nil.

I can provide more code file also which is require.

As I have already said you need to debug or show us the code where you
tell the individual line item that it owns that particular
expensescounter (or the code where you tell the expenses that it
belongs to that particular line item, whichever way you do it).

Presumably somewhere you have called current_expensescounter to create
it, but then what have you done with it?

Colin

As I have already said you need to debug or show us the code where you
tell the individual line item that it owns that particular
expensescounter (or the code where you tell the expenses that it
belongs to that particular line item, whichever way you do it).

Presumably somewhere you have called current_expensescounter to create
it, but then what have you done with it?

I have used current_expensescounter method in application_controller.rb
file.

Then current_expensescounter use in create method of
line_items_controller.rb file to add selected quantity to
expensescounter. like as follow:

def create
    @expensescounter = current_expensescounter
    quantity = Quantity.find(params[:quantity_id])
    @line_item = @expensescounter.add_quantity(quantity.id)
    @line_item.quantity = quantity
.
.
.
.
.
end

In above code i used add_quantity, that method i define in
expensescounter.rb file like as follow:

def add_quantity(quantity_id)
    current_item = line_items.find_by_quantity_id(quantity_id)
    if current_item
       current_item.expense += 1
    else
       current_item = line_items.build(quantity_id: quantity_id)
    end
    current_item
  end

I still don't see the code that adds @expensescounter to @line_item.
Can you point to a particular line that does this?

Colin

I still don't see the code that adds @expensescounter to @line_item.
Can you point to a particular line that does this?

That code is not used.

What i done is that i add expense column to line_items table. Then i
generate migration like as follow:

" rails generate migration combine_items_in_expensescounter "

In migration file i used following code:

class CombineItemsInExpensescounter < ActiveRecord::Migration
  def up
    Expensescounter.all.each do |expensescounter|
       sums =
expensescounter.line_items.group(:quantity_id).sum(:expense)
       sums.each do |quantity_id, expense|
       if expense > 1

       expensescounter.line_items.where(quantity_id:quantity_id).delete_all

          item = expensescounter.line_items.build(quantity_id:
quantity_id)
          item.expense = expense
          item.save!
        end
      end
    end
  end

  def down
  end
end

This was working. I can able to add quantity to expensescounter and one
by one price of that quantity add in that.

BUT when i was create association between user and expensescounter model
this problem occur.

I still don't see the code that adds @expensescounter to @line_item.
Can you point to a particular line that does this?

That code is not used.

I don't understand you. The problem you originally posted was that in
@line_item.expensescounter
expensescounter was nil, with lineitem belongs to expensescounter.
For that code to work you need to tell @line_item needs to know that
it owns the expensescounter object. Where have you done that? Or are
you now talking about a new problem that you have not fully described?
If so then please start again, describe the problem carefully and
show us the code that does not work (just the bit that does not work).
By debugging you should have tied that down to just a few lines of
code.

Colin