# Noob DRY method question

**URL:** <https://www.ruby-forum.com/t/noob-dry-method-question/174408>\
**Category:** Rails\
**Created:** [August 26, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408 "2009-08-26T15:06:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 26, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/1 "2009-08-26T15:06:58Z")

</div>

Hi Gurus, I’m learning the Rails framework, and I have this exact  
block of html/javascript in two places in an if/else construct in  
application.html.erb:

Trying to be DRY, I’d like to put that html/javascript somewhere once,  
and call it from within application.html.erb. What’s the best place  
to put it, and how do I call it from application.html.erb?

TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 26, 2009, 3:21pm UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/2 "2009-08-26T15:21:36Z")

</div>

The best place to put helper methods for views is in… well…  
helpers. In this case, in helpers/application\_helper.rb

If you are using layouts, it’s probable you’re using a variant of

\<%= yield %\>, in which case it would be messy to change the declaration for the inline javascript. I'd suggest adding it as a javascript code in the header of the templates that need to use it. Just add a \<%= yield :extra\_headers %\> declaration in the header of your layout if not already doing so. Seems you are using prototype,

Event.observe($$(‘body’), ‘load’, function() {  
Modalbox.show(…)  
});

will do the trick

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 26, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/3 "2009-08-26T19:21:10Z")

</div>

Thanks super. It occurred to me that I have this in the wrong place–  
I should have it in the template where the user logs in. Which brings  
me to another noob question:

Under javascript, I’d do something like

Click here

In my sessions controller I have

\<%= submit\_tag ‘Log in’ %\>

What’s the proper syntax to mix in the javascript call into the  
submit\_tag?

_Many_ TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 26, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/4 "2009-08-26T19:31:08Z")

</div>

Oops, meant to say “In my sessions view I have…”

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 27, 2009, 11:00am UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/5 "2009-08-27T11:00:44Z")

</div>

The API documentation will help with these. Bookmark  
[http://api.rubyonrails.org](http://api.rubyonrails.org)

> **[ActionView::Helpers::FormTagHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001739)**
>
> Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [August 27, 2009, 11:06am UTC](https://www.ruby-forum.com/t/noob-dry-method-question/174408/6 "2009-08-27T11:06:35Z")

</div>

Thanks super. I’m still getting used to this framework, which  
includes figuring out where and how it’s documented 🙂
