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
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?
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…
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.