Search

Sara Trice

Just a programmin', bellydancin', cake bakin' kinda girl

Category

tech

RTFM, Ruby on Rails edition

Since the happymapper google group has been closed to posting, thought I’d toss this up here.

I posted this question to the group:

---------------------------------------------------
I have a node that looks like this:
<ItemInfo>
 <ProductVar text_id="Variable1">MyValue</ProductVar>
 <ProductVar text_id="Variable2">MySecondValue</ProductVar>
</ItemInfo>

I need both the value of the attribute "text_id" and the element value
"MyValue". I was trying to use something like what I saw in the
"current weather" example:

<aws:current-condition icon="http://deskwx.weatherbug.com/images/
Forecast/icons/cond007.gif">Sunny</aws:current-condition>
element :current_condition, String, :tag => 'current-
condition', :attributes => {:icon => String}

Which (the example) works just fine on my system. So this is the
mapping I have:

class ItemInfo
 include HappyMapper
 tag 'ItemInfo'
 has_many :product_var, String, :tag => "ProductVar", :attributes =>
{:text_id => String}
end

This does not return text_id. I can return
item_info.product_var.text_id if I use "element" in place of
"has_many", but there are always going to be multiple "ProductVar"s,
so that won't work. Whenever I try to replace "element" with
"has_many", I get this error:

undefined method `attribute_nodes' for ["text_id", "Variable1"]:Array

Ideas?
---------------------------------------------------

Well, if I’d thought this through and read the code, as suggested here:
http://railstips.org/blog/archives/2010/10/14/stop-googling/
I’d have figured this out. Since I didn’t, it took the help of both the wonderful Eric Larson and Damien Le Berrigaud to point out my epic failure to read the docs (in the nicest way possible).

Eric pointed out that what I needed was to make ProductVar its own class:

---------------------------------------------------
From: Eric Larson
Date: October 22, 2010 10:07:13 AM CDT

Hi Sara,

When I'm happymapping, I like to create a class per each element I'm
parsing, always.

Try something like this:

class Product
 include HappyMapper
 tag 'ProductVar'
 attribute :text_id, String
 content :value
end

class ItemInfo
 include HappyMapper
 tag 'ItemInfo'
 has_many :products, Product
end

- - - -

Pseudo code... but it should be very close to working.

Cheers,
- Eric
---------------------------------------------------

Which I thought was brilliant, but then realized I didn’t have the method “content” available to me because I was using the nokogiri-happymapper gem. So off to Damien I went, to ask if he could merge in the “content” method to nokogiri-happymapper, only to have him tell me:

---------------------------------------------------
You can already do that with text_node. Check the spec: it "should parse text node correctly" in happymapper_spec :)
---------------------------------------------------

So, had I just read the docs and/or the spec, I’d have figured this out. SMRT.

Thought I’d post this just in case anyone else had the same problem.

Advertisements

Ruby: how to split a string into equal chunks

“some long test string or other”.scan(/.{15}/)

Snagged from http://snippets.dzone.com/posts/show/5621 and posted here so I can find it later.

RubyGems 1.3.7 doesn’t play nice with OSX 1.5.8

If you ever run into this pain in the butt error, hopefully this’ll save you some time:

Installing a gem without the version works fine; installing a gem using “-v 2.x.x” or whatever version you like ends in:
spec_fetcher.rb:254: warning: getc is obsolete; use STDIN.getc instead

Guess what? RubyGems 1.3.7 doesn’t play nice with OSX 10.5.8. So downgrade to 1.3.6, or if you’re using RVM, do this:
echo 'rubygems_version=1.3.6' >> ~/.rvm/config/user ; rvm remove 1.8.7 ; rvm install 1.8.7
(assuming you are using 1.8.7 in your RVM, otherwise, use 1.9.1 or whatever)
Voila, works like a charm.

EDIT 7/7/2011: According to Mike Shaheen, the new way to do this is simply:  rvm rubygems 1.3.5

Thanks to Wayne E. Seguin for his help on irc.freenode.net #rvm (and for RVM in the first place!)

PS You may have to downgrade to 1.3.5 to get “rake gems:install” to work.

Images showing negative in Safari, and not displaying in IE.

This problem drove me crazy for a while:

I did a website for recently an established webcomic and some of the old archive images were showing up in negative on some browsers, and not showing up at all in others. But it didn’t happen to all the images, and not on all browsers. I beat my head against this for a while.

When you can’t figure out what’s wrong with an image, you look at the image properties, rather than beat your head against a wall. This is what I suggest, anyway, because I didn’t do that.

They were CMYK.

CMYK images show as negative in Safari, and don’t show up at all in some versions of IE.

Yay. Great. Converted them to RGB, re-uploaded, all fixed.

Ruby on Rails: When Textmate Breaks

After installing Ruby 1.8.7 on my shiny iMac, Textmate decided that it no longer knew where the Ruby environment was. Oh, it knew where Ruby was, and would run Ruby scripts just fine, but whenever I tried to use anything from any bundle, all I got was:

“env: ruby: No such file or directory”

Maddening.

Turns out I didn’t have a environment.plist file in my /Users/(username)/.MacOSX/ directory, and even though it worked just fine before without it, that was no longer the case.

Here’s where to get a nifty little Ruby script to build it and even stick it in the right place for you. Note, the file must already exist, so you’ll have to make a .MacOSX directory in your user directory, and create an empty file “environment.plist.”

Ruby on Rails: Autocomplete with jQuery error: “Couldn’t find FileData with ID”

Just in case anyone else has this problem and does not want to bang their head against the wall for an hour like I did:

When using the autocomplete plugin that uses jQuery instead of scriptaculous, you need to add a route:

map.auto_complete ':controller/:action', 
     :requirements => { :action => /auto_complete_for_S+/ }, 
     :conditions => { :method => :get }

As per this post by the author of the plugin, who for some unknown reason hasn’t included that step with the instructions for the plugin. (WTF?) Otherwise it just sends the request to the show action, which looks at you stupidly and replies, “Couldn’t find FileData with ID=auto_complete_for_yourfieldname”, replacing “yourfieldname” with whatever you’d actually like it to return.

Create a free website or blog at WordPress.com.

Up ↑