Rails options_for_select with include blank and selected option

If you want to use a select_tag and include a blank option it can be achieved using the following snippet:

<%= select_tag “district”, options_for_select(Customer.all.collect { |k| ["#{k.district} #{k.store}", k.id] }.insert(0, ["", 0]), params[:number].to_i) %>

- it also handles setting the selected option if one is passed – in this case in the query string. I have been blogging about this previously but noticed that I had forgotten to show how to pass the chosen option if one exists.

ruby multiple gsub

This is a code snippet I use, it’s from an oreilly book – 100% copy I’m not taking credits.

class String
  def multisub(key_value_pairs=[].freeze)
    regexp_fragment = key_value_pairs.collect { |k, v| k }
    gsub(Regexp.union(*regexp_fragment)) do |m|
      key_value_pairs.detect { |k, v| k =~ m }[1]
    end
  end
end

and this is how it’s being used: “Dætte er æn tækst med % i og æ ø å”.multisub([[/æ/i, 'ae'], [/å/i, 'aa'], [/ø/i, 'oe'], [/%/i, '-procent']]). It replaces æ ø å and % and produces a nice string instead.

Ruby Wurfl Detect User Agents

I decided to rewrite the entire class so that it would better fit my requirements. Instead of using it like I described previously, I wrote a new test case for it: Continue reading

Ruby and WURFL detecting user agents

I decided to move away from the whole levenhstein approach to detecting user agents and instead focus on using WURFL and trust that my visitors user agents will be in WURFL instead of focusing on handling those who aren’t. Continue reading

Ruby WURFL implementation approach

I know that I will be using WURFL in my current project, but how would I prefer implement it? Good question. I could wrap it all into an object and then pass the request as a parameter, the object would then handle all of the logic for me. Continue reading

Follow

Get every new post delivered to your Inbox.