I''m trying to distinguish between a url that passes an id value such as: /localhost:3000/person/new/3 and one that doesn''t such as: /localhost:3000/person/new/ Can anyone tell me what the Ruby for "If a parameter has been passed"? Many thanks in advance! -- Posted via http://www.ruby-forum.com/.
Is this what you''re looking for? doSomething if @params[:id].nil? On 23-Nov-05, at 6:31 PM, Nick Ce wrote:> I''m trying to distinguish between a url that passes an id value > such as: > /localhost:3000/person/new/3 > > and one that doesn''t such as: > /localhost:3000/person/new/ > > Can anyone tell me what the Ruby for "If a parameter has been passed"? > Many thanks in advance! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
dteare wrote:> Is this what you''re looking for? > > doSomething if @params[:id].nil?Thanks for the reply - that looks really promising, but I can''t quite get it to work. I''m writing a really simple method that I want to behave differently if a parameter is passed to it. At present it looks like: def new @person = Person.new @addedcompany = Company.find(params[:id]) end I only need the @addedcompany variable to be assigned if @params[:id] exists. So far I''ve tried this based on your suggestion but with no joy: def new @person = Person.new @addedcompany = Company.find(params[:id]) if !@params[:id].nil? end -- Posted via http://www.ruby-forum.com/.
this should work....I don''t believe you need the @ infront of the params as in your previous code def new @person = Person.new @addedcompany = Company.find(params[:id]) unless params[:id].nil? end Cheers, Steven -- Posted via http://www.ruby-forum.com/.
Steven wrote:> this should work....I don''t believe you need the @ infront of the params > as in your previous code > > def new > @person = Person.new > @addedcompany = Company.find(params[:id]) unless params[:id].nil? > end > > Cheers, > StevenFantastic - that''s working very well now. Thanks you both for your help. As an aside, would someone be kind enough to explain when the use of @ in front of a variable is required, and when it isn''t? -- Posted via http://www.ruby-forum.com/.
AFAIK the @ before the variable indicates that it is an instance variable, while @@ is used to access a class variable. I believe that you don''t need the @ if you used attr_reader/writer. On 11/24/05, Nick Ce <graphis1-ee4meeAH724@public.gmane.org> wrote:> Steven wrote: > > this should work....I don''t believe you need the @ infront of the params > > as in your previous code > > > > def new > > @person = Person.new > > @addedcompany = Company.find(params[:id]) unless params[:id].nil? > > end > > > > Cheers, > > Steven > > > Fantastic - that''s working very well now. Thanks you both for your help. > As an aside, would someone be kind enough to explain when the use of @ > in front of a variable is required, and when it isn''t? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
martin wrote:> AFAIK the @ before the variable indicates that it is an instance > variable, while @@ is used to access a class variable. I believe that > you don''t need the @ if you used attr_reader/writer.back in the days params was an instance variable...so i should be addressed as @params...now params is a method call and should be addressed as params... the old way still works, but use the new method...goes for session too... -- Posted via http://www.ruby-forum.com/.