I need to check if a parameter is set so that I can build some information for my application, but No matter how I format my if statement in the file " Application.rb" it return the following error. You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.[] Here is the line if params[:day] So we have a nil I know that, its why I am checking to see if it''s empty or not. Here is the funny part just a little lower on the page we have *Parameters*: {"month"=>"04", "day"=>"04", "year"=>"2002", "section"=>"metro"} so params[:day] should be available. I have also tried just "params" and "@params" and several other ways all with the same result. Have also tried == and != with the if all resulting in a nil error. is there some thing that would cause the params hash to not be available in the application.rb file? any help would be appreciated. Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/fb690992/attachment.html
Spectre013 wrote:> I need to check if a parameter is set so that I can build some > information for my application, but No matter how I format my if > statement in the file "Application.rb" it return the following error. > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.[] > > > Here is the line > > if params[:day] >I have no idea if it is correct, but I always check using if @params.key?("day") Mark Farver
On 1/12/06, Mark Farver <mfarver@ticom.com> wrote:> > Spectre013 wrote: > > > > Here is the line > > > > if params[:day] > > > >I have no idea if it is correct, but I always check using > > > >if @params.key?("day") > > > >Mark FarverTried that and ended up with You have a nil object when you didn''t expect it! The error occured while evaluating nil.key? which is looking like @params is empty. would there be some reason the params hash is not set or available to the application.rb?? _______________________________________________> Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/2860b9f5/attachment.html
Just as an answer to this if any one has a similar problem it seem that the @prams is not available to the initialize method. I moved my code out to the application_helper.rb and was able to get it workin ok. Hope this helps Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/45dd2fe9/attachment.html
Spectre013 wrote:> I need to check if a parameter is set so that I can build some > information for my application, but No matter how I format my if > statement in the file "Application.rb" it return the following error. > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occurred while evaluating nil.[] > > > Here is the line > > if params[:day] > > So we have a nil I know that, its why I am checking to see if it''s > empty or not. > > Here is the funny part just a little lower on the page we have > > *Parameters*: {"month"=>"04", "day"=>"04", "year"=>"2002", > "section"=>"metro"} > > so params[:day] should be available. > > > I have also tried just "params" and "@params" and several other ways > all with the same result. Have also tried == and != with the if all > resulting in a nil error. > > is there some thing that would cause the params hash to not be > available in the application.rb file? > > any help would be appreciated. > Brian >I am sure there is a shorter way of doing this but @day_param = @params[''day''] @day = @day_param.inspect if @day == "nil" Dan
On 13/01/2006, at 11:45 AM, Daniel Wright wrote:> @day_param = @params[''day''] > @day = @day_param.inspect > if @day == "nil"A hash always returns nil for an unknown key. All objects have a nil? method. Try this: if @params[''day''].nil? -- Phillip Hutchings phillip.hutchings@sitharus.com
Spectre013 wrote:> Here is the line > > if params[:day]Spectre013, I was having a similar problem testing for the existence of a param[] in my controller. I solved it like this: if !defined? params[:id] || params[:id].nil redirect_to :action => ''list'' else @foobar = FooBar.find(params[:id]) end Maybe this helps. Matt -- Matt C. Wagner Information Security Analyst Network Intrusion Detection Security Operations Center Corporate Information Security Wells Fargo Bank
Phillip Hutchings wrote:> > On 13/01/2006, at 11:45 AM, Daniel Wright wrote: > >> @day_param = @params[''day''] >> @day = @day_param.inspect >> if @day == "nil" > > A hash always returns nil for an unknown key. All objects have a nil? > method. Try this: > > if @params[''day''].nil? > > > -- > Phillip Hutchings > phillip.hutchings@sitharus.com > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Hi Phillip, Not meaning to hijack this thread, I figured that my problem/solution was relevant to the OP''s. How would I incorporate your example into my existing code? specifically the method under the else statement? def index @category = @params[''category_id''] @categories = @category.inspect if @categories == "nil" @items = Item.find_all else @items = Item.find_all("category_id="+@category) end end Dan
On 1/12/06, Phillip Hutchings <sitharus-rails@sitharus.com> wrote:> A hash always returns nil for an unknown key.Off topic, but just FYI, nil is only the default value for an unknown key: irb(main):006:0> foo = Hash.new(:foo) => {} irb(main):007:0> foo[:bar] => :foo Hash.new can even take a block, which gets called when an unknown key is accessed. See http://www.ruby-doc.org/core/classes/Hash.html#M000650.
> def index > @category = @params[''category_id''] > @categories = @category.inspect > if @categories == "nil" > @items = Item.find_all > else > @items = Item.find_all("category_id="+@category) > end > end > > > Dandef index @category = params[''category_id''] @items = Item.find_all unless @category @items = Item.find_all_by_category(@category) if @category end The params hash may be empty, but the most likely explanation is that params[:day] != params["day"] _Kevin -- Posted via http://www.ruby-forum.com/.
> > if !defined? params[:id] || params[:id].nil > redirect_to :action => ''list'' > else > @foobar = FooBar.find(params[:id]) > end >This might also work... ... redirect_to :action => ''list'' unless params[:id] @foobar = FooBar.find(params[:id]) ... _Kevin -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich wrote:>> def index >> @category = @params[''category_id''] >> @categories = @category.inspect >> if @categories == "nil" >> @items = Item.find_all >> else >> @items = Item.find_all("category_id="+@category) >> end >> end >> >> >> Dan >> > > def index > @category = params[''category_id''] > @items = Item.find_all unless @category > @items = Item.find_all_by_category(@category) if @category > end > > >With this I get the error: "*Undefined method `find_all_by_category'' for Item:Class*" I changed the third line back to: Item.find_all("category_id="+@category) which made it work, and I shortened the method by 3 lines of code. Thanks, Dan
Daniel Wright wrote:> Kevin Olbrich wrote: >>> >> > With this I get the error: > > "*Undefined method `find_all_by_category'' for Item:Class*" > > I changed the third line back to: > > Item.find_all("category_id="+@category) > > which made it work, and I shortened the method by 3 lines of code. > > Thanks, > > DanIf you are going to use that form, use this instead to avoid SQL injection... Item.find_all(:conditions=>["category = ?", @category]) I''m not sure why the dynamic finder doesn''t work in your case.... _Kevin -- Posted via http://www.ruby-forum.com/.
On 1/12/06, Daniel Wright <dw@wonderwave.net> wrote:> > Kevin Olbrich wrote: > >> def index > >> @category = @params[''category_id''] > >> @categories = @category.inspect > >> if @categories == "nil" > >> @items = Item.find_all > >> else > >> @items = Item.find_all("category_id="+@category) > >> end > >> end > >> > >> > >> Dan > >> > > > > def index > > @category = params[''category_id''] > > @items = Item.find_all unless @category > > @items = Item.find_all_by_category(@category) if @category > > end > > > > > > > With this I get the error: > > "*Undefined method `find_all_by_category'' for Item:Class*" > > I changed the third line back to: > > Item.find_all("category_id="+@category) > > which made it work, and I shortened the method by 3 lines of code. > > Thanks, > > DanI have it woking no problem with the inital call to the method not being inside of the initalize method. EXAMPLE: class StoriesController < ApplicationController def intialize if @params[:id] .........more code here end end will throw the nil error, I am not 100% sure but it seems to me that the params hash is not set up when the initialize method is called. I have since moved it to a helper and it works just fine. In fact that was a better place for it i think. If some on knows or finds out about the params and the initialize method it would be nice to find out. I will keep looking. Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060113/112cbaa1/attachment.html
Spectre013 wrote:> On 1/12/06, Daniel Wright <dw@wonderwave.net> wrote: > will throw the nil error, I am not 100% sure but it seems to me that the > params hash is not set up when the initialize method is called.This is probably correct. The initialize method is your object constructor. I bet if you did somehting like this it would work... def initialize super if params[:id] .... end That might let the base classes set up the hash before you tried to use it. _Kevin -- Posted via http://www.ruby-forum.com/.
On Jan 12, 2006, at 4:14 PM, Spectre013 wrote:> > > On 1/12/06, Daniel Wright <dw@wonderwave.net> wrote: Kevin Olbrich > wrote: > >> def index > >> @category = @params[''category_id''] > >> @categories = @category.inspect > >> if @categories == "nil" > >> @items = Item.find_all > >> else > >> @items = Item.find_all("category_id="+@category) > >> end > >> end > >> > >> > >> Dan > >> > > > > def index > > @category = params[''category_id''] > > @items = Item.find_all unless @category > > @items = Item.find_all_by_category(@category) if @category > > end > > > > > > > With this I get the error: > > "*Undefined method `find_all_by_category'' for Item:Class*" > > I changed the third line back to: > > Item.find_all("category_id="+@category) > > which made it work, and I shortened the method by 3 lines of code. > > Thanks, > > Dan > > > > I have it woking no problem with the inital call to the method not > being inside of the initalize method. > > EXAMPLE: > > class StoriesController < ApplicationController > def intialize > if @params[:id] > .........more code here > end > end > > will throw the nil error, I am not 100% sure but it seems to me > that the params hash is not set up when the initialize method is > called. > > I have since moved it to a helper and it works just fine. In fact > that was a better place for it i think. > > If some on knows or finds out about the params and the initialize > method it would be nice to find out. > > I will keep looking. > > BrianBrian- The initialize method of a controller is outside of the request response cycle so it doesn''t know anything about params. You can get around this by using filters. Set it up like this: class StoriesController < ApplicationController before_filter :do_stuff def do_stuff # do stuff with params... end end Cheers- -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Thanks Kevin...> If you are going to use that form, use this instead to avoid SQL > injection... > > Item.find_all(:conditions=>["category = ?", @category]) > > I''m not sure why the dynamic finder doesn''t work in your case.... > > _Kevin > >
On 1/12/06, Matt.C.Wagner@wellsfargo.com <Matt.C.Wagner@wellsfargo.com> wrote:> > Spectre013 wrote: > > Here is the line > > > > if params[:day] > > Spectre013, > > I was having a similar problem testing for the existence of a param[] in > my controller. I solved it like this: > > if !defined? params[:id] || params[:id].nil > redirect_to :action => ''list'' > else > @foobar = FooBar.find(params[:id]) > end > > Maybe this helps. > > MattStill throws a nil error when called in the initialize method, thanks for the example will keep it in mind as I learn Rails. Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060113/2a20801a/attachment.html