can not redirect to nil

38 views
Skip to first unread message

Jaimin Pandya

unread,
Aug 2, 2014, 6:51:01 AM8/2/14
to rubyonra...@googlegroups.com
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


--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Aug 2, 2014, 7:04:52 AM8/2/14
to rubyonra...@googlegroups.com
On 2 August 2014 11:50, Jaimin Pandya <li...@ruby-forum.com> wrote:
> 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

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

Jaimin Pandya

unread,
Aug 2, 2014, 8:21:21 AM8/2/14
to rubyonra...@googlegroups.com
> 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.

Colin Law

unread,
Aug 2, 2014, 8:39:17 AM8/2/14
to rubyonra...@googlegroups.com
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

Jaimin Pandya

unread,
Aug 2, 2014, 8:54:12 AM8/2/14
to rubyonra...@googlegroups.com
> 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.

Jaimin Pandya

unread,
Aug 4, 2014, 4:53:00 AM8/4/14
to rubyonra...@googlegroups.com
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.

Scott Ribe

unread,
Aug 4, 2014, 8:39:46 AM8/4/14
to rubyonra...@googlegroups.com
On Aug 4, 2014, at 2:52 AM, Jaimin Pandya <li...@ruby-forum.com> wrote:

> I can not able to find solution of this error.
>
> Any help would be appreciate.

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.


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Colin Law

unread,
Aug 4, 2014, 4:13:21 PM8/4/14
to rubyonra...@googlegroups.com
On 4 August 2014 09:52, Jaimin Pandya <li...@ruby-forum.com> wrote:
> 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.

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

Colin

Jaimin Pandya

unread,
Aug 5, 2014, 12:31:29 AM8/5/14
to rubyonra...@googlegroups.com
> 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?

Jaimin Pandya

unread,
Aug 5, 2014, 12:34:07 AM8/5/14
to rubyonra...@googlegroups.com
> 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?

Colin Law

unread,
Aug 5, 2014, 3:34:24 AM8/5/14
to rubyonra...@googlegroups.com
On 5 August 2014 05:30, Jaimin Pandya <li...@ruby-forum.com> wrote:
>> 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.

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

Jaimin Pandya

unread,
Aug 5, 2014, 4:47:43 AM8/5/14
to rubyonra...@googlegroups.com
> 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

Colin Law

unread,
Aug 5, 2014, 5:01:48 AM8/5/14
to rubyonra...@googlegroups.com
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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bcbb3bf8a4a75ed671469a229623eb1e%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Jaimin Pandya

unread,
Aug 5, 2014, 5:46:33 AM8/5/14
to rubyonra...@googlegroups.com
> 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.

Colin Law

unread,
Aug 5, 2014, 6:12:12 AM8/5/14
to rubyonra...@googlegroups.com
On 5 August 2014 10:45, Jaimin Pandya <li...@ruby-forum.com> wrote:
>> 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

Jaimin Pandya

unread,
Aug 5, 2014, 7:00:41 AM8/5/14
to rubyonra...@googlegroups.com
> 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

Colin Law

unread,
Aug 5, 2014, 8:35:55 AM8/5/14
to rubyonra...@googlegroups.com
I still don't see the code that adds @expensescounter to @line_item.
Can you point to a particular line that does this?

Colin

Jaimin Pandya

unread,
Aug 5, 2014, 11:05:56 AM8/5/14
to rubyonra...@googlegroups.com
> 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.

Colin Law

unread,
Aug 5, 2014, 11:27:42 AM8/5/14
to rubyonra...@googlegroups.com
On 5 August 2014 16:05, Jaimin Pandya <li...@ruby-forum.com> wrote:
>> 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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a8ab68ce0c6aec174a64fb8e22a8e768%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages