ruby

Rails multipart Post Problem

Hi I have the below code in my controller <% form_for @picture, :picture, :url=>pictures_url, :html => { :multipart => true } do | form | %> <%=form.file_field :image%> <%=submit_tag 'Save'%> <%end%> which submits to the below controller class PicturesController < ApplicationController def create render :text=>1 end end ...

how to remove leading and trailing non-alphabetic characters in ruby

I want to remove any leading and trailing non-alphabetic character in my string. for eg. ":----- pt-br:-" , i want "pt-br" Thanks ...

Is there a way to check if a record was built by another model in active record?

hey, When using accepts_nested_attributes_for, I got stuck when having a validation which required the original to be present. The code will help clear up that sentence. class Foo < ActiveRecord::Base has_one :bar accepts_nested_attributes :bar end class Bar < ActiveRecord::Base #property name: string belongs_to :foo valida...

Can one YAML object refer to another?

I'd like to have one yaml object refer to another, like so: intro: "Hello, dear user." registration: $intro Thanks for registering! new_message: $intro You have a new message! The above syntax is just an example of how it might work (it's also how it seems to work in this cpan module.) I'm using the standard ruby yaml parser. Is t...

Ruby/LDAP non-ASCII character support

It seems like LDAP requires strings with non-ASCII characters to be Base64 encoded. The way to tell it that a string is to be parsed as a base64 encoded string is to add an extra colon to the attribute name such that "cn: name" becomes "cn:: name" (according to this site). Now, my question is: How do I tell Ruby LDAP to do this? I could...

How do I build this JSON object in ruby?

I need to bring an array of ruby objects in JSON. I will need to find the item in the JSON object by id, so I think it is best that the id is the key of each object. This structure makes the most sense to me: { "1": {"attr1": "val1", "attr2": "val2"}, "2": {"attr1": "val1", "attr2": "val2"}, "3": {"attr1": "val1", "a...

Need for Rails plugin management tools?

Hello, I've been searching for a while, and I can't find any modern rails plugin management tools. I found several gem management tools (such as bundler and isolate), but no plugin management tools. The closest thing to that I found was piston, and that's not exactly what I was looking for was it was for plugin svn:externals managemen...

How do I call a grand-parent's method, and skipping the parent in ruby

How do I choose a particular a method call in the inheritance chain? class A def boo; puts "A:Boo"; end end class B < A def boo; super; puts "B:Boo"; end end class C < B def boo; self.A.boo(???); puts "C:Boo"; end end Thus the output would be A:Boo, C:Boo TIA, -daniel ...

Bash scripting "common gotchas" for Python/Perl/Ruby programmers

Background: I grew up on using Perl/Python/Ruby for sysadmin-type tasks and shell scripting. I always avoided Bash scripting whenever I needed anything programmer-ish, like functions, looping or control structures. Back then, I could pick my favorite tool for whatever the job. Problem: Now I am working in a situation where the preferr...

Ruby version of to String method

This question is about formatting ruby's strings. In Python, built-in data structures have a built-in to-string method, and so when a variable is printed, the string is conveniently formatted to be reflective of the data structure used. For example: >>>$ python Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) [GCC 4.4.1] on linux2 Type...

Working on my first json from scratch and I can't seem to figure this simple part out..

HAML: = link_to 'redeem', redeem_admin_organization_path(organization), :class => 'button_short live redeem' Controller: def redeem @organization = Organization.find(params[:id]) @organization.update_attribute('accumulated_credits', '0') end redeem.js.haml: == $("#organization_#{@organization.id} .redeem").html("#{escape_javas...

Rails 2 - partials: what does @comment = Comment.new mean?

I am working through a tutorial with the following code: <h3>New Comment</h3> <%= render :partial => @comment = Comment.new, :locals => { :button_name => "Create" } %> I believe that 'render :partial => @comment' works like 'render :partial => "comment", :object => @comment' Where does ' = Comment.new' fit in? Is it shorthand f...

proper way to run raw sql queries with sequel

I am not clear yet on the proper way to run raw sql queries with sequel currently I am trying this but not sure if it is the correct way DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1") do |row| @zonename = row end What would be good is if I can run the queries as raw then access the results like normal e.g i...

MongoMapper - Updating existing records with new keys

When adding a key to an existing model (with existing data) via MongoMapper, I can create new documents with the new key but when trying to access existing documents using that same key it fails stating that it's an "undefined method." I was wondering if anyone had any insight. Thanks in advance! (Yes, these examples are truncated.) ...

Using sinatra, how do I construct a proper DateTime value from 3 separate input boxes?

How do I take a numeric month, day and year from three separate text inputs and parse a proper value for my DateTime field in the db? ...

How to do this in Ruby?

I have an array(list?) in ruby: allRows = ["start","one","two","start","three","four","start","five","six","seven","eight","start","nine","ten"] I need to run a loop on this to get a list that clubs elements from "start" till another "start" is encountered, like the following: listOfRows = [["start","one","two"],["start","three","fou...

Whats the fasted way to extract an array of nested objects from an array of objects in Ruby ?>

I have an array of Elements, and each element has a property :image. I would like an array of :images, so whats the quickest and least expensive way to achieve this. Is it just iteration over the array and push each element into a new array, something like this: images = [] elements.each {|element| images << element.image} ...

Converting filesize string to kilobyte equivalent in Rails

Hello, My objective is to convert form input, like "100 megabytes" or "1 gigabyte", and converts it to a filesize in kilobytes I can store in the database. Currently, I have this: def quota_convert @regex = /([0-9]+) (.*)s/ @sizes = %w{kilobyte megabyte gigabyte} m = self.quota.match(@regex) if @sizes.include? m[2] eval("se...

Which is faster to learn: Django (Python) or Ruby on Rails (Ruby)?

I have done the front-end of my design, but I have no experience in web programming. Would like to pick up a language asap so that I can deploy the product. Which is faster to pick up between the two? I know there is always debate in which is better. I think either one serves well for me, but I want to know which one is easier to learn, ...

Using polymorphic paths with nested associations.

I have a polymorphic association that looks like this: class Line < ActiveRecord::Base belongs_to :item, :polymorphic => true end class Education < ActiveRecord::base has_many :lines, :as => :item end class Work < ActiveRecord::base has_mayn :lines, :as => :item end I'd like a simple way to create a new Line from the parent...