Ruby for "If url contains id"

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!

Is this what you’re looking for?

 doSomething if @params[:id].nil?

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

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

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?

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…

Thanks for the clarification folks - that’s helped me to get my head
round it.

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.