# How would u do this?

**URL:** <https://www.ruby-forum.com/t/how-would-u-do-this/142040>\
**Category:** Rails\
**Created:** [July 2, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040 "2008-07-02T20:58:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 2, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/1 "2008-07-02T20:58:41Z")

</div>

Pardon the newbism 🙂 I’ve inhereted this bit of code and it is being  
used in a couple of rhtml files. I’d like to avoid duplicate code by  
pulling this out somehow - how would you do this? This is an ecommerce  
app and the code is for the credit card expiration date fields.

`
<%= text_field "orderdetail","month",:class
=>'graymonth',:value =>'mm',:size => '4',:MAXLENGTH =>
2,:onfocus=>"if(this.value=='mm') this.value='';" %> <% if
@is_expiration_month_error != nil and @is_expiration_month_error ==
'false' %>/ <%= text_field "orderdetail","year",:class
=>'graymonth',:value=>'yyyy',:size => '8',:MAXLENGTH =>
4,:onfocus=>"if(this.value=='yyyy') this.value='';" %><% end %>
`

Thanks.

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 2, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/2 "2008-07-02T21:09:15Z")

</div>

On Jul 2, 7:57 pm, nahabed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Pardon the newbism 🙂 I’ve inhereted this bit of code and it is being  
> used in a couple of rhtml files. I’d like to avoid duplicate code by  
> pulling this out somehow - how would you do this? This is an ecommerce  
> app and the code is for the credit card expiration date fields.

You could extract it into a partial you share accross the app.

Fred

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 2, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/3 "2008-07-02T23:13:39Z")

</div>

good idea 🙂 thanks.

Now, I have a partial \_foo living under app/views/user and I am trying  
to call it from inside bar.rhtml which lives under app/views/purchase

I am getting “Couldn’t find template file for purchase/\_foo” error

How to get around this?

On Jul 2, 12:08 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 2, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/4 "2008-07-02T23:21:15Z")

</div>

On Jul 2, 10:12 pm, nahabed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> good idea 🙂 thanks.
> 
> Now, I have a partial \_foo living under app/views/user and I am trying  
> to call it from inside bar.rhtml which lives under app/views/purchase
> 
> I am getting “Couldn’t find template file for purchase/\_foo” error
> 
> How to get around this?

Specify the path to the template, ie render :partial =\> ‘user/  
foo’ (might need a leading / there, can’t remember). I sometimes put  
shared partials in a folder called something like shared, so that it  
doesn’t look like they belong more to one thing than another.

Fred

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 3, 2008, 4:44am UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/5 "2008-07-03T04:44:29Z")

</div>

Frederick C. wrote:

> I sometimes put  
> shared partials in a folder called something like shared, so that it  
> doesn’t look like they belong more to one thing than another.
> 
> Fred

In my “persnickety-ness”, I take that approach one step further by have  
an entire directory structure for partials that mirrors the views folder  
(and sometimes includes further division). So if I have

app/views  
– order  
– person  
– product

I’ll also have

app/views/partials  
– order  
– person  
– product

It might sound like a lot to manage, but since I tend to have a boat  
load of partials, it is very well organized and incredibly easy to share  
across different resources where they might be needed.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 3, 2008, 9:56am UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/6 "2008-07-03T09:56:55Z")

</div>

> app/views/partials  
> – order  
> – person  
> – product

Do you use the same approach when you have nested resources??

> It might sound like a lot to manage, but since I tend to have a boat  
> load of partials, it is very well organized and incredibly easy to share  
> across different resources where they might be needed.

Do you put ALL partials in there? or only the shared?

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 3, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/7 "2008-07-03T16:23:43Z")

</div>

“Wolas!” wrote:

> > app/views/partials  
> > – order  
> > – person  
> > – product
> 
> Do you use the same approach when you have nested resources??

There is some “debate” about the usefulness of nested resources. I  
started to use them, but decided that it was better for me to not. So I  
map an invoice resource separately from a customer resource even though  
an invoice cannot exist independently. So in my partials subfolder, I  
would have folders for customer partials and invoice partials.

> > It might sound like a lot to manage, but since I tend to have a boat  
> > load of partials, it is very well organized and incredibly easy to share  
> > across different resources where they might be needed.
> 
> Do you put ALL partials in there? or only the shared?

I started with having only shared ones in there, but I found that I did  
not like having partials in two places, so I moved all of them into that  
structure. Now I know where they are without having to think about  
whether they are shared or not.

Web Blogger mentioned using a nested folder depending on the purpose of  
the partials. I do this too. For example, I have a wizard process for  
adding players, coaches, referees, etc to a sports system. These  
processes happen via a sports controller, so in my folder structure, I  
have

app/views/sports  
app/views/partials/sports  
app/views/partials/sports/wizard

(I wrote the wizard in such a way that the same partials are used  
regardless of whether it’s a player, coach, or referee).

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 3, 2008, 1:58pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/8 "2008-07-03T13:58:25Z")

</div>

Ya I prefer making a shared or a partials folder i my views where in  
I would keep the partials which I use very often ,we can also create a  
folder as a nested folder depending upon the purpose.

## JON

---

<div class="post-metadata">

**Author:** ![nahabed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b77776/32.png) [@nahabed](https://www.ruby-forum.com/u/nahabed)\
**Post date:** [July 3, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/how-would-u-do-this/142040/9 "2008-07-03T21:18:53Z")

</div>

Thanks for the explanation and your time.

best

j

On Jul 3, 3:23 pm, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
