Re: active admin undefined method `[]'' for nil:NilClass

Colin Law
Thu, 19 Apr 2012 20:10:52 -0000

On 19 April 2012 18:34, ruby LED
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:
> im working with active admin i have collection_select or dropdown box
> that populates the packages and crews im just wondering i why this
>
> undefined method `[]'' for nil:NilClass`
> i dont know what happened but it run few days ago and tested it once but
> now it raises this error are there possible way to solve this?
>
> def create
>   @package = Package.find(params[:package_crew][:package_id])
>   crew = Crew.find(params[:package_crew][:crew_id] )
>   @package_crew = @package.add_crew(crew.id)
>   if @package_crew.save
>      redirect_to {admin_package_crew_url}
>   end
>  end
> thanks in advance more power to us all
>
> i tried this
>
> def create
>  #pid = params[:package_crew][:crew_id] if params[:package_crew]
>  pid,cid = 2 #tried nil,0..10
>  if params[:package_crew]
>     pid = params[:package_crew][:package_id]
>     cid = params[:package_crew][:crew_id]
>  end
>  @package = Package.find_by_id(pid)
>  crew = Crew.find_by_id(cid)
>  #@package = Package.find(params[:package_crew][:package_id])
>  #crew = Crew.find(params[:package_crew][:crew_id] )
>  @package_crew = @package.add_crew(crew.id)
>  if @package_crew.save
>     redirect_to {admin_package_crew_url}
>  end
> end
>
> add_crew method
>
> def add_crew(crew_id)
>    current_package = package_crews.find_by_crew_id(crew_id)
>    if current_package
>      current_package.quantity = current_package.quantity.to_i + 1
>    else
>      current_package = package_crews.build(:crew_id => crew_id)
>      current_package.price = current_package.crew.price
>    end
>      current_package
>  end
> moving parameters
>
> {"utf8"=>"✓",
>
 "authenticity_token"=>"n4WZpQpES6mnSO12oGKV/MeoxAVvV9OmJQKfhcpnvQM=",
>
 "crew_for_package"=>{"package_id"=>"1",
>  "crew_id"=>"1",
>  "quantity"=>"1"},
>  "commit"=>"Create Package crew"}
> stack error
>
> app/admin/package_crews.rb:35:in `create''

The error says that on line 35 you are calling [] on something that is nil.
Please tell us which is line 35, that may make it easier to find the
problem.  First, however, have a look at the rails guide on debugging
to get ideas on how to debug the code and work out what the problem
is.

Colin

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at