Forum: Ruby on Rails How to solve can not redirect to nil error?

1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-20 09:44
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/expen...

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.
F7141e066d691d7f18765c1ea6389b18?d=identicon&s=25 Serguei Cambour (javix)
on 2014-08-20 09:53
(Received via mailing list)
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 ?
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-08-20 10:04
(Received via mailing list)
On 20 August 2014 08:44, Jaimin Pandya <lists@ruby-forum.com> 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
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-20 10:04
> 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
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2014-08-20 15:08
(Received via mailing list)
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 Jones
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-24 09:25
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])}"
      rescue ActiveRecord::RecordNotFound
      expensescounter = Expensescounter.create
      session[:expensescounter_id] = expensescounter.id
      expensescouter
     end
end

Any help would be appreciated.
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-08-24 09:57
(Received via mailing list)
On 24 August 2014 08:25, Jaimin Pandya <lists@ruby-forum.com> 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
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-24 13:44
Attachment: show.html.erb (738 Bytes)
Colin Law wrote in post #1155814:
> On 24 August 2014 08:25, Jaimin Pandya <lists@ruby-forum.com> 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.
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-08-24 14:33
(Received via mailing list)
On 24 August 2014 12:44, Jaimin Pandya <lists@ruby-forum.com> wrote:
> Colin Law wrote in post #1155814:
>> On 24 August 2014 08:25, Jaimin Pandya <lists@ruby-forum.com> 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
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-25 08:13
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
1eb28f3c91b0a7b8bfb1a72cfa8befaa?d=identicon&s=25 Jaimin Pandya (jaimin)
on 2014-08-25 09:07
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.
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2014-08-25 09:32
(Received via mailing list)
On 25 August 2014 08:07, Jaimin Pandya <lists@ruby-forum.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.