# :Partial and :locals question

**URL:** <https://www.ruby-forum.com/t/partial-and-locals-question/172232>\
**Category:** Rails\
**Created:** [July 17, 2009, 4:52am UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232 "2009-07-17T04:52:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 17, 2009, 4:52am UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/1 "2009-07-17T04:52:50Z")

</div>

Hello,

I want to be able to call a partial with and without a local.  
E.g.:  
1 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
:parent\_form =\> contact\_form, :phone =\> Phone.new} %\>

2 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
:parent\_form =\> contact\_form, :phone =\> Phone.new, :child\_index =\> “111”  
} %\>

In my partial I have:

\<% if child\_index -%\>  
Do something  
\<% else -%\>  
Do something else  
\<% end -%\>

The problem is that, when I don’t pass the local child\_index, the  
partial don’t render and give me a error. What is the problem?

Thanks,  
David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 17, 2009, 5:06am UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/2 "2009-07-17T05:06:28Z")

</div>

Hello…  
the solution:  
[http://hackd.thrivesmarthq.com/how-to-make-a-rails-partial-with-optional-locals-parameters](http://hackd.thrivesmarthq.com/how-to-make-a-rails-partial-with-optional-locals-parameters)

I’m still looking for a dryer way.

Regards,

David S.

David S. wrote:

> Hello,
> 
> I want to be able to call a partial with and without a local.  
> E.g.:  
> 1 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
> :parent\_form =\> contact\_form, :phone =\> Phone.new} %\>
> 
> 2 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
> :parent\_form =\> contact\_form, :phone =\> Phone.new, :child\_index =\> “111”  
> } %\>
> 
> In my partial I have:
> 
> \<% if child\_index -%\>  
> Do something  
> \<% else -%\>  
> Do something else  
> \<% end -%\>
> 
> The problem is that, when I don’t pass the local child\_index, the  
> partial don’t render and give me a error. What is the problem?
> 
> Thanks,  
> David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 17, 2009, 6:46am UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/3 "2009-07-17T06:46:58Z")

</div>

what about using

\<% unless child\_index.blank? -%\>  
Do something  
\<% else -%\>  
Do something else  
\<% end -%\>

Thank you

Rodrigo D.

David S. wrote:

> Hello,
> 
> I want to be able to call a partial with and without a local.  
> E.g.:  
> 1 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
> :parent\_form =\> contact\_form, :phone =\> Phone.new} %\>
> 
> 2 - \<%= render :partial =\> “/contacts/phones/create”, :locals =\> {  
> :parent\_form =\> contact\_form, :phone =\> Phone.new, :child\_index =\> “111”  
> } %\>
> 
> In my partial I have:
> 
> \<% if child\_index -%\>  
> Do something  
> \<% else -%\>  
> Do something else  
> \<% end -%\>
> 
> The problem is that, when I don’t pass the local child\_index, the  
> partial don’t render and give me a error. What is the problem?
> 
> Thanks,  
> David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 19, 2009, 10:07pm UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/4 "2009-07-19T22:07:03Z")

</div>

what about using blank?

On 17 jul, 00:06, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 19, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/5 "2009-07-19T22:00:01Z")

</div>

what about using blank?

On 17 jul, 00:06, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 20, 2009, 6:27am UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/6 "2009-07-20T06:27:20Z")

</div>

Thanks for the replays…

unless child\_index.blank? Did not work.

But: if defined?(child\_index) && child\_index, worked ok.

thks again.

David S.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 19, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/partial-and-locals-question/172232/7 "2009-07-19T22:11:10Z")

</div>

Sorry just replied to you directly and not to the group…

Use the defined? method like…

\<% if defined?(child\_index) && child\_index %\>

-Evan Worley

On Jul 16, 9:46 pm, Rodrigo D. \<rails-mailing-l…@andreas-
