Some interesting findings from web-dev land…
To reset the key values in a PHP array you can do this:
1234567891011121314151617181920212223$things = array(’a',’b',’c',’d');
unset($things[0]);
print_r($things);
#outputs
#Array
#(
# [1] => b
# [2] => c
# [3] => d
#)
# Actually do the reset.
$things = array_values($things);
print_r($things);
#outputs
#Array
#(
# [0] => b
# [1] => c
# [2] => d
#)
This might be handy for anyone that’s using the Ruby on Rails will_paginate plugin/gem. It’s a helper that displays the result totals, current viewing range and page range based on an input Will Paginate collection array.
1234567891011def render_pagination_totals(will_paginate_collection=false)
if will_paginate_collection and !will_paginate_collection.total_entries.zero?
if will_paginate_collection.total_entries < (will_paginate_collection.current_page * will_paginate_collection.per_page)
end_of_range = will_paginate_collection.total_entries
else
end_of_range [...]
Don’t forget kids, error_messages_for seems only to like a symbol or a string of the instance variable name of your instantiated Active Record object (model). Not the object itself.
So this doesn’t work:
1<%= error_messages_for(@instance_variable_name) %>
Neither does this but then again it shouldn’t! (blogging for my own records):
1<%= error_messages_for(’active_record_model_class_name’) %>
This is what you want:
1<%= error_messages_for(:instance_variable_name) %>
A string [...]
Managed to create a dynamic select menu with this code today. It includes a blank first value:
1<%= select("event", "search_type", EventType.find(:all).collect {|et| [et.name, et.id] }.sort, { :include_blank => ‘all’ }) %>
I had an integer field in a database table today (start age). It needed displaying as a select menu containing a number range within Active Scaffold. I thought for a while about creating a new model called age and creating a relationship, then I thought nah, that’s overkill. Anyway I figured out how to hard-code [...]
To create a Hash from a couple of Arrays you can do this in Ruby. I’m using a couple of Array ranges in this example.
OK the keys (strings):
1keys = (’1′..’99′).to_a
Now the values (integers):
1values = (1..99).to_a
Now the instantiation of the Hash object from the 2 arrays:
1hash = Hash[*keys.zip(values).flatten]
I installed the Hpricot gem today from the Hpricot GitHub homepage. I tried to parse some XML and got this error:
1uninitialized constant SearchController::Hpricot
Suddenly realised I should of required the library but the install documentation didn’t mention that. Managed to fix the issue with this line in my environment.rb
1config.gem "hpricot"
It was, (not 100% up on WAG2 yet) a W3C WAI accessibility recommendation to nest your form fields (inputs/selects etc) inside your label tags. So they were contained by the label much like you’d put an <img> tag inside an <a> tag to make it a link.
The Rails helpers don’t seem to have much [...]
Following on from my last post, sorry folks a bit more Rails bashing. What happened to convention over configuration and the principle of least surprise?
So to add a plain old class attribute to a form tag using the form_for helper you need to do (notice this time we’re declaring the actual html options with the [...]
Some days I love Rails, some days it really frustrates me.
Anyways spent 10 minutes remembering the syntax for the form_for helper to add a regular class attribute. The second hash of parameters are the HTML ones! Thought I’d blog it for my own records:
123<% form_tag( { :action => "destroy", :method => "delete" }, :class => [...]