how to do ruby try/catches
file = open(“/tmp/some_file”, “w”)
# … write to the file …
# ex: rescue Timeout::Error, Errno::ECONNREFUSED
# orÂ rescue Exception => exception
# … handle the exceptions …
file.close # … and this always happens.
ActiveRecord…blah blah blah (i.e. the beginning of a model/whatever.rb file)
self.id = 3
To run unit tests
first run rake db:test:prepare
then ruby test\unit\whatever.rb
or rake test (to run all tests everywhere).
in application.rb add
return User.find(1)# TODO search for it much better, login...
now you can use it thus:
@user_blended_cast.user_id = getCurrentUser().id
and voila it works!
So typically in RoR you pass one object from page to page (in my opinion you do). You are passing the parameters to create “one” new object, etc.
Now what happens when you want to pass in two objects?
You can re-create it by passing in parameters which will re-create and mimic the “one object” way…
It’s a little contorted…
Here’s an example:
<%= hidden_field 'filter', "id", :value => filter.id %>
This generates filter[id] = value yeesh!
To use it:
How to use an “interior” field to specify another DB entry
<%= text_field 'filter_assignment', 'parameter1_setting', "size" => 10, :value => 61 %>
now in the “receiving” model of the post…
newFilterLink = FilterAssignment.new(params[:filter_assignment])
so we setup and used a new “db” entry by generating its parameters. This is just barely nuts, but works. A little bit contorted, as mentioned. Good luck!
So…say you’re in RoR and you want to pass to yourself some specific value to a hidden part of a form? Something that doesn’t totally match a DB entry, in the name?
(This shows you how to specify the starting value, which also applies to other types besides hidden fields, BTW)
<%= hidden_field ‘filter’, “id”, :value => “newvalue” %>
This generates filter[id] = newvalue in the HTML.
What this actually does is allows you to setup an “arbitrary” value to pass into the controller.Â Typically with RoR
<%= hidden_field ‘filter_to_use’, “id” %>
Generates <code>filter_to_use[id] = filter_to_use.id </code> in the HTML (I think). This is of course not useful if filter_to_use is a variable name that is just a variable you want to pass in to the next controller (i.e. it doesn’t exist).Â So you need to use the <code> :value => whatever </code>above.
To use it in the receiving controller:
@filter = Filter.find(params[:filter_to_use][:id]) # retarded
What this does is looks up your parameter. So that’s a little contorted, but works, once you know how to set the value.
So…say you want to write a Ruby function to “sanitize” file names so that users can’t input file names and overwrite other files, or save theirs in odd places, etc?
Turns out that
? * / \ | < >
And the following are sensitive in Linux:
! & ; : ” ‘ ~ `
(at least for commands run in bash) so…
sanitizedName = @user_blended_cast.name.gsub(/[`\.?*\/\\|<>!&;:"'~@#\$%\^\(\)]/, '_')
Which replaces those bad boys with an underscore. So that’s my attempt at a file sanitize function so you can use it on the command line, etc.
If you have normal confirmations, generated by RoR in IE, and it does nothing…have no fear. It might be that the instance of IE on that computer is broken and other work well. Happened to me!
So it turns out that if you require a ruby file that is in your local directory, it doesn’t work. It loads the file and the class names, but leaves out all of the functions, etc.
To get it to work
require ‘./localfile’ works.
Also note there is a discreapncy between require and load, which is interesting. load _requires_Â the “.rb” (at least for me) while require does not. A minor inconsistency.
To run a “foreign” command in python:
os.system(“dir”) # or os.system(“ls -l”)