Noob DRY method question

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:

<body onload="Modalbox.show('<div class=\'warning\'> <p> <center> <%= flash[ :notice ] %> </center> </p> <p> <center> <input type=\'button\' value=\'OK\' onclick=\'Modalbox.hide()\'/> </center> </p> </div>', { title: this.title, width: 300 } );">

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

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 <body><%= yield %></body>, 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

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

<a href="#" class="demo-btn" title="JavaScript Object demo" onclick="Modalbox.show(node, {title: this.title, width: 300, afterLoad: setObservers, onHide: removeObservers }); return false;">Click here</a>

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

Oops, meant to say "In my sessions view I have..."

The API documentation will help with these. Bookmark http://api.rubyonrails.org

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001739

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