# Ruby for "If url contains id"

**URL:** <https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740>\
**Category:** Rails\
**Created:** [November 24, 2005, 12:31am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740 "2005-11-24T00:31:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 12:31am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/1 "2005-11-24T00:31:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 12:42am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/2 "2005-11-24T00:42:57Z")

</div>

Is this what you’re looking for?

```
 doSomething if @params[:id].nil?
```

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 12:52am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/3 "2005-11-24T00:52:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 1:04am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/4 "2005-11-24T01:04:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 9:45am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/5 "2005-11-24T09:45:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 11:13am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/6 "2005-11-24T11:13:16Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 11:17am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/7 "2005-11-24T11:17:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![n1c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ec8ea/32.png) [@n1c](https://www.ruby-forum.com/u/n1c)\
**Post date:** [November 24, 2005, 10:14am UTC](https://www.ruby-forum.com/t/ruby-for-if-url-contains-id/48740/8 "2005-11-24T10:14:39Z")

</div>

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.
