Rails 3: nested resources, shallow routes, form_for

Was banging my head against this problem until I read this and the comment from “tekwiz”.

Solution:

Call form_for in your views then render the _form.html.erb partial, passing the form object created by “form_for” as a local variable, i.e., when rendering children/new.html.erb form you need the parent and the child objects, however when rendering the children/edit.html.erb form (with shallow routes), you do not need the parent resource. Instead of trying to insert logic into the children/_form.html.erb partial to determine if the form is being invoked from a “new” or an “edit” action, simply invoke form_for in the associated view, then render the partial.

in children/new.html.erb :

 <h1>New Child Resource</h1>

 <%= form_for([@parent, @child]) do |f| %>

  <%= render ‘form’, :f => f %>

  <% end %>

  <%= link_to ‘Show’, child_path(@child) %>

and in children/edit.html.erb :

 <h1>Edit Child Resource</h1>

 <%= form_for(@child) do |f| %>

  <%= render ‘form’, :f => f %>

  <% end %>

  <%= link_to ‘Show’, child_path(@child) %>

OK, from the "you all probably know this" department…

Ever had a script execute just fine from your own shell, but fail to run when executed as a cron job?  Sometimes the problem is that the cron environment differs from your user environment.

One can determine the cron job environment by simply running a cron job that executes /bin/env , e.g.,

$ cat env_dump.sh

#!/bin/bash

export PATH=/sbin:/bin:/usr/sbin:/usr/bin

export MAILTO=<your e-mail address here>

/bin/env


Then edit your crontab to execute the script:

$ crontab -e 

# set to a time a few minutes from now

# in this case, I edited the crontab at 08:43 and set the cron job to execute at 08:45 

45 08 * * * /path/to/env_dump.sh

Check your e-mail and compare and contrast against the output from $ /bin/env run from a user shell.

Capturing *nix exit status when using Expect

Help from here:

http://www.tek-tips.com/viewthread.cfm?qid=1400140&page=5

and here:

http://www.rhinocerus.net/forum/lang-tcl/26055-how-get-return-value-spawned-process-expect-script.html

The script ./exit_fail_3.sh is a simple BASH script that just sleeps for 5 seconds then exits with a value 3.

dl-oak:~ $ expect

expect1.1> spawn ./exit_fail_3.sh

spawn ./exit_fail_3.sh

14853

expect1.2> set result [wait]

14853 exp7 0 3

expect1.3> lindex $result 0

14853

expect1.4> lindex $result 1

exp7

expect1.5> lindex $result 2

0

expect1.6> lindex $result 3

3

expect1.7> 

The UNIX process exit status is:

lindex $result 3

emacs linum-mode

In linum-version “0.9wza”

To add some spaces after the line number in emacs linum-mode, you can tweak this line in linum-update-window:  (concat “%” (number-to-string w) “d”)))))

e.g., 

(concat “%” (number-to-string w) “d  ”)))))

will add two spaces between the line number and your code.
http://web.student.tuwien.ac.at/~e0225855/linum/linum.el