Posted by Chirag Patel on September 12, 2007
Partial basics
Some different things about passing parameters to partials
- Basics of passing parameters (i.e. locals) into partials – Why are parameters for partials (
locals) passed differently than params for urls?!? Don’t know, but basically use the locals hash in both the render call and use the name of the local in the partial itself. This link also shows how to explicitly check for local parameter in case it’s nil
- Making a partial use optional parameters – Basically, set the parameter to
nil inside the partial (e.g. title = nil unless defined?(title))
- Why passing locals in partials is preferred over @variables – Partials can see the
@variables of its parent view, but instead pass the variables as parameters for clarity in complex code bases
Debugging partials
- be sure to look at console log (and/or development log), it will show the parameters being passed
Posted in Ruby on Rails | Leave a Comment »
Posted by Chirag Patel on September 12, 2007
Styles can be dynamically changed in 4 ways:
<a href="" style="font-size:91%;">
<style type="text/css">
body { color: #FFFFFF; }
</style>
<link rel="stylesheet" type="text/css" href="layout.css" />
Posted in HTML/CSS, Javascript | Leave a Comment »
Posted by Chirag Patel on September 12, 2007
I decided to use BackgrounDrb 0.2.1 to launch a periodic task that would run every 15 seconds.Warning: After going through the hassle of setting up BackgrounDrb, I found out that it isn’t even supported in Windows (my dev environment)! See step 8. So, these instructions haven’t been fully tested yet on a *.nix environment. Good luck and please post comments if you have any helpful suggestions.
- Install BackgroundDrb:
svn co http://svn.devjavu.com/backgroundrb/tags/release-0.2.1 backgroundrb
- Setup BackgroundDrb:
rake backgroundrb:setup
- Generate worker thread (
Foo is capitalized): ruby script/generate worker Foo
- In one of your controllers, create new worker with
Middleman object (:foo_worker is lowercase with underscore)
MiddleMan.new_worker(:class => :foo_worker,
:args => "Arguments used to instantiate a new HeartratepostWorker object",
- Instantiate
Middleman object in environment.rb (you might not need this with version 0.2.1 of BackgrounDrb)
require "drb" DRb.start_service
MiddleMan = DRbObject.new(nil, "druby://localhost:22222"
- Install the daemons gem if you haven’t already:
gem install daemons
- Install the slave gem if you haven’t already:
gem install slave
- Start the BackgroundDrb server in a separate command window:
ruby script\backgroundrb start
- If you get the error “
ERROR: there is already one or more instance(s) of the program running”, delete log\backgroundrb.pid
- Windows users are out of luck! You’ll get this message:
`fork': the fork() function is unimplemented on this machine. Here’s a message from Ezra (the devleoper of BackgroundDrb) about Windows support:
- “Unfortunately that won’t cut it in this case, I wish it would. Slave does more then just fork. It uses ipc and a few other things that just don’t work yet on windows. There is a possibility it could be made to work but right now it does not, even with win32 process (gem). -Ezra Wed Nov 29 16:19:03 EST 2006″
- Debugging methods if things don’t work well
Notes
- You might want to restart the backgroundrb server after you’ve modified the
*_worker.rb file to lib/workers. It could cause an error otherwise.
- In case you get this error when starting Mongel, copy the
vendor/plugins/backgroundrb directory to a temp directory and it will work
already initialized constant OPTIONS
- To set up a periodic task, set up the
Middleman object this way:
session[:job_key] = worker
MiddleMan.schedule_worker(
:class => :heartrate_post_worker,
:args => "some arg to do_work",
:job_key => :simple_schedule,
:trigger_args => {
:start => Time.now,
:end => Time.now + 10.minutes,
:repeat_interval => 15.seconds
}
)
References
- Sending emails in the background with ActionMailer and BackgrounDRB
- Example using TTL (time to live)
Posted in Ruby on Rails | 2 Comments »