How to pass in a ‘new’ DB setting through fields in RoR

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!

How to use hidden values

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?

Here’s how:

(This shows you how to specify the starting value, which also applies to other types besides hidden fields, BTW)

<code>

<%= hidden_field ‘filter’, “id”, :value => “newvalue” %>

</code>
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

<code>

<%= hidden_field ‘filter_to_use’, “id” %>

</code>

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.

symbols disallowed in windows filenames

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

Windows disallows:

? * / \ | < >

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.

Ruby require in local directory

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

instead of

require ‘localfile’

require ‘./localfile’ works.

Go figure.

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.

How to create headers or footers “system wide” for all pages in RoR

To create a header/footer “system wide” for all pages:

go to app\views\layouts
delete everything (unless you want a certain controller’s worth of pages to NOT have the header/footer), then create a file called “application.rhtml”
One way to do so is rename one of the old one’s (that you delete) as application.rthml. That will now serve as the “layout” for all views, into which file you can create your default header/footer.

If you want a generic header for each file (since we just deleted all the headers) put

 <title><%= controller.class %> <%= controller.action_name %></title>

in the layout and rename it application.rhtml and delete all the others and you have a perfect header/footer system.

Roger's meanderings, notes to himself, bug reports, and other things