Forum: Ruby on Rails Ruby for "If url contains id"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick C. (Guest)
on 2005-11-24 01:31
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!
dteare (Guest)
on 2005-11-24 01:42
(Received via mailing list)
Is this what you're looking for?

     doSomething if @params[:id].nil?
Nick C. (Guest)
on 2005-11-24 01:52
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
Steven (Guest)
on 2005-11-24 02:04
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
Nick C. (Guest)
on 2005-11-24 10:45
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 (Guest)
on 2005-11-24 11:14
(Received via mailing list)
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.
Mikkel (Guest)
on 2005-11-24 12:13
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...
n1c (Guest)
on 2005-11-24 12:17
Thanks for the clarification folks - that's helped me to get my head
round it.
This topic is locked and can not be replied to.