How to solve can not redirect to nil error?

68 views
Skip to first unread message

Jaimin Pandya

unread,
Aug 20, 2014, 3:45:44 AM8/20/14
to rubyonra...@googlegroups.com
I am using Ruby 1.9.3 and Rails 3.2.16. I follow "Agile web development
with rails" book.

I am trying to make **Expense Management** application.

I am providing Heroku links as follow:
" https://expense-management-app.herokuapp.com/manageexpense "

In above link, When I click on "Add to count" button then I can able to
redirect to

"https://expense-management-app.herokuapp.com/expensescounters/6"

this link. It is working.

Now i create association between user and expensescounter to see the
expenses for authorize user.

>>After creating association, when I click on "Add to count" button, I got error
like:

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

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

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

>> Code of line_items_controller.rb :

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

respond_to do |format| #line no. : 51
if @line_item.save
format.html { redirect_to @line_item.expensescounter } #line no. :
54
.
.
.
.
.
end

How can I solve this problem?

Any help would be appreciated.

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

Javix

unread,
Aug 20, 2014, 3:52:21 AM8/20/14
to rubyonra...@googlegroups.com
Can you create a gist with the code to repsoduce the error or post  the full error stack trace, please ?

Colin Law

unread,
Aug 20, 2014, 4:03:27 AM8/20/14
to rubyonra...@googlegroups.com
On 20 August 2014 08:44, Jaimin Pandya <li...@ruby-forum.com> wrote:
> ...
>>>After creating association, when I click on "Add to count" button, I got error
> like:
>
> ActionController::ActionControllerError (Cannot redirect to nil!):
>
> app/controllers/line_items_controller.rb:54:in `block (2 levels) in
> create'
>
> app/controllers/line_items_controller.rb:51:in `create'
>
>>> Code of line_items_controller.rb :
>
> def create
> @expensescounter = current_expensescounter
> quantity = Quantity.find(params[:quantity_id])
> @line_item = @expensescounter.add_quantity(quantity.id)
> @line_item.quantity = quantity
>
> respond_to do |format| #line no. : 51
> if @line_item.save
> format.html { redirect_to @line_item.expensescounter } #line no. :
> 54
> .
> end
>
> How can I solve this problem?

By doing some debugging. The error indicates that
@lineitem.expensescounter is nil (the clue is that it says cannot
redirect to nil) so you must find why this is the case. Look in your
code to see where you set that up and work out why it is nil. A
simple way of debugging to to insert lines like
logger.info( some_expression )
which will insert text into development.log (and in the server
window). You can use that to look at values and work out what is
going wrong.

Colin

Jaimin Pandya

unread,
Aug 20, 2014, 4:05:14 AM8/20/14
to rubyonra...@googlegroups.com
> Can you create a gist with the code to repsoduce the error or post the
> full error stack trace, please ?

I am providing full error trace in following link:

> http://pastebin.com/VXTmSN1b

Matt Jones

unread,
Aug 20, 2014, 9:06:51 AM8/20/14
to rubyonra...@googlegroups.com
You're going to need to figure out why @line_item.expensescounter is nil. Post the code here if you need help finding the problem. 

--Matt Jones

Jaimin Pandya

unread,
Aug 24, 2014, 3:26:12 AM8/24/14
to rubyonra...@googlegroups.com
Matt Jones wrote in post #1155588:
> Code of application_controller.rb:

class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper

private
def current_expensescounter
Expensescounter.find(session[:expensescounter_id])
logger.info "expensescounter_id is
#{(session[:expensescounter_id])}"
rescue ActiveRecord::RecordNotFound
expensescounter = Expensescounter.create
session[:expensescounter_id] = expensescounter.id
expensescouter
end
end

Any help would be appreciated.

Colin Law

unread,
Aug 24, 2014, 3:56:24 AM8/24/14
to rubyonra...@googlegroups.com
On 24 August 2014 08:25, Jaimin Pandya <li...@ruby-forum.com> wrote:
> Matt Jones wrote in post #1155588:
>> On Wednesday, 20 August 2014 03:45:44 UTC-4, Ruby-Forum.com User wrote:
>> You're going to need to figure out why @line_item.expensescounter is
>> nil.
>> Post the code here if you need help finding the problem.
>
>> Code of application_controller.rb:
>
> class ApplicationController < ActionController::Base
> protect_from_forgery
> include SessionsHelper
>
> private
> def current_expensescounter
> Expensescounter.find(session[:expensescounter_id])
> logger.info "expensescounter_id is
> #{(session[:expensescounter_id])}"

With the logger.info line here you have changed what this function
returns, as now it will return the result from logger.info, not from
the find. Move that line above the find call, then put another logger
call in the rescue.

> rescue ActiveRecord::RecordNotFound
> expensescounter = Expensescounter.create
> session[:expensescounter_id] = expensescounter.id
> expensescouter

That should be expensescounter not expensescouter, but that would give
a different error if it ran so I presume that line of code has never
been executed.

> end
> end
>
> Any help would be appreciated.

I am assuming that neither of the problems I have noted above is
causing the actual problem (as I assume you put the logger call in to
try and diagnose it.

We went all round this last time you tried to ask this question a
little time ago. You must show us the code where you set
@line_item.expensescounter. The above code does not do that, it is
just a private method of the controller that returns an
Expensescounter object. If you do not understand what I am asking
then please say so.

Colin

Jaimin Pandya

unread,
Aug 24, 2014, 7:44:46 AM8/24/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1155814:
Which code should i show you that i am not understand.

Colin Law

unread,
Aug 24, 2014, 8:33:23 AM8/24/14
to rubyonra...@googlegroups.com
You have a variable called @line_item and are trying to reference the
member @line_item.expensescounter. I assume that @line_item is an
object of class LineItem. Is that correct?

Assuming the above is correct then what is LineItem.expensescounter?
Is it an association, so in the lineitem class you have lineitem
belongs_to Expensescounter or has_one Expensescounter? If so then at
some point you should have made an expensescounter and assigned it to
a lineitem object and saved them both in the database. Your error
suggests that the lineitem object in @line_item does not have an
associated expensescounter. So the code you need to show is how you
create the expensescounter and then give it to the lineitem. All you
have showed so far is a public method (current_expensescounter) which
finds or creates one, but there is nothing to say that it belongs to
the particular lineitem referenced by @lineitem.

Colin

Jaimin Pandya

unread,
Aug 25, 2014, 2:14:12 AM8/25/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1155829:
> You have a variable called @line_item and are trying to reference the
> member @line_item.expensescounter. I assume that @line_item is an
> object of class LineItem. Is that correct?

Yes, it is correct.

> Assuming the above is correct then what is LineItem.expensescounter?
> Is it an association, so in the lineitem class you have lineitem
> belongs_to Expensescounter or has_one Expensescounter? If so then at
> some point you should have made an expensescounter and assigned it to
> a lineitem object and saved them both in the database. Your error
> suggests that the lineitem object in @line_item does not have an
> associated expensescounter. So the code you need to show is how you
> create the expensescounter and then give it to the lineitem. All you
> have showed so far is a public method (current_expensescounter) which
> finds or creates one, but there is nothing to say that it belongs to
> the particular lineitem referenced by @lineitem.

I have created relationship between line items, expensescounter and
quantity.

It is an association between lineitem and expensescounter. I am
providing code of model:

> Code of cart.rb:

class Expensescounter < ActiveRecord::Base
attr_accessible :user_id

has_many :line_items, dependent: :destroy
belongs_to :user

validates :user_id, presence: true
default_scope order: 'expensescounters.created_at DESC'
.
.
.
end

> code of line_item.rb:

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

belongs_to :quantity
belongs_to :expensescounter
.
.
.
end

> code of quantity.rb:

class Quantity < ActiveRecord::Base
.
.
has_many :line_items

before_destroy :ensure_not_referenced_by_any_line_item
.
.
.
end

Jaimin Pandya

unread,
Aug 25, 2014, 3:07:52 AM8/25/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1155829:
> Assuming the above is correct then what is LineItem.expensescounter?
> Is it an association, so in the lineitem class you have lineitem
> belongs_to Expensescounter or has_one Expensescounter? If so then at
> some point you should have made an expensescounter and assigned it to
> a lineitem object and saved them both in the database. Your error
> suggests that the lineitem object in @line_item does not have an
> associated expensescounter. So the code you need to show is how you
> create the expensescounter and then give it to the lineitem. All you
> have showed so far is a public method (current_expensescounter) which
> finds or creates one, but there is nothing to say that it belongs to
> the particular lineitem referenced by @lineitem.

In your last message, you given clue to solve this error.

> Your error suggests that the lineitem object in @line_item does not have > an
associated expensescounter

This is main clue which help me to solve this error. By trying to solve
this error i learn many things.

Thank you very much to you for helping me and you spent valuable time.

I solved this error because of you.

Again Thank you very much.

Warm regards.

Colin Law

unread,
Aug 25, 2014, 3:31:51 AM8/25/14
to rubyonra...@googlegroups.com
On 25 August 2014 08:07, Jaimin Pandya <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1155829:
> ..
> In your last message, you given clue to solve this error.
>
>> Your error suggests that the lineitem object in @line_item does not have > an
> associated expensescounter
>
> This is main clue which help me to solve this error. By trying to solve
> this error i learn many things.
>
> Thank you very much to you for helping me and you spent valuable time.
>
> I solved this error because of you.
>
> Again Thank you very much.

Glad to of help.

Colin
Reply all
Reply to author
Forward
0 new messages