How to solve can not redirect to nil error?

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.

On Wednesday, August 20, 2014 9:45:44 AM UTC+2, Ruby-Forum.com User
wrote:

redirect to
like:
def create
.
Posted via http://www.ruby-forum.com/.

Can you create a gist with the code to repsoduce the error or post the
full error stack trace, please ?

On 20 August 2014 08:44, Jaimin P. [email protected] wrote:

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

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:

Full error trace - Pastebin.com

On Wednesday, 20 August 2014 03:45:44 UTC-4, Ruby-Forum.com User wrote:

redirect to
like:
def create
.
.
.
.
end

How can I solve this problem?

Any help would be appreciated.

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 J.

Matt J. 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])}”
rescue ActiveRecord::RecordNotFound
expensescounter = Expensescounter.create
session[:expensescounter_id] = expensescounter.id
expensescouter
end
end

Any help would be appreciated.

On 24 August 2014 08:25, Jaimin P. [email protected] wrote:

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

Colin L. wrote in post #1155814:

On 24 August 2014 08:25, Jaimin P. [email protected] wrote:
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.

I have attached view file named show.html.erb. Please fine it.

When i click on “Add to count” button, then new page look like above
view.

On 24 August 2014 12:44, Jaimin P. [email protected] wrote:

Colin L. wrote in post #1155814:

On 24 August 2014 08:25, Jaimin P. [email protected] wrote:
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.

Which code should i show you that i am not understand.

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

Colin L. 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

On 25 August 2014 08:07, Jaimin P. [email protected] wrote:

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

Colin L. 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.