ruby

Fetching only the first record in the table (via the view) without changing controller?

I am trying to only fetch the first record in my table for display. I am creating a site where a user can upload multiple images and attach to a post but I only want to display the first image view for each post. For further clarification posts belong_to projects. So when you are on the projects show page you see multiple posts. In thi...

Socket.recv works but not gets or read?

Hello I've been messing around with Sockets in Ruby some and came across some example code that I tried modifying and broke. I want to know why it's broken. Server: require "socket" dts = TCPServer.new('127.0.0.1', 20000) loop do Thread.start(dts.accept) do |s| print(s, " is accepted\n") s.write(Time.now) print(s, "...

To check the default select value tag!

I have a question.Here is the code! f.select(:departments,Department.all.collect{|c|[c.name,c.id]},{},:size=>10,:multiple => ture) class Emergency has many :departments end the html source like this: <select id="emergency_departments" multiple="multiple" name="emergency[departments][]" size="10"><option value ="">""</opt...

Ruby GPX file parser

Advise what can parse GPX file in Ruby? I tried: http://github.com/dougfales/gpx - it does not work with Ruby Enterprise Edition (http://github.com/dougfales/gpx/issues # issue / 1) I would not like to write a parser ...

Ruby Debug IDE error : ruby-debug-ide-0.4.9/lib/ruby-debug-ide.rb:109:in `debug_load'

I hope someone can assist me. I have RubyMine 2.0.2 installed on Windows 7 32 bit computer. Since a week ago (I presume it must have been after I have update some gems) I cant seem to debug form the IDE. I am trying to debug a rake task which I could before. Running the rake task normally works perfect, just debug doesnt. Its not just li...

Route alias, is it possible?

I have a Vehicle model: Routes: map.resources :vehicles, :has_many => :suppliers Everything works great, but Vehicle has a boolean attribute is_truck. I want to make an Alias so I can get the same resources filtering only trucks, I tried with: Routes: map.trucks '/trucks', :controller => :vehicles, :action => :index, :is_truck => t...

Polymorphic Paperclip Interpolations

I'm using the Polymorphic fork of Paperclip in Rails, but have been having some massive problems with regards to the overwriting of unique filenames. No matter whether I put a time-stamp (more on that in a second) or the id of the asset in the URL, if a file with the same name is uploaded subsequently, then the previous one is overwritte...

Ruby: Allowing a module to have settings

If I'm building a library in ruby, what's the best way to allow users of the library to set module-wide settings that will be available to all sub classes etc of the library? A good example would be if I'm writing a library for posting to a webservice: TheService::File.upload("myfile.txt") # Uploads anonymously TheService::Settings.lo...

Access a variable in a controller from another controller

Hello all. I am looking for a way to access some variables which I assigned values in one controller(say: controller A) in another controller(say: controller B). One of the controllers contains my main project but the other is where I implemented 'Date Picker'. i want to move the date from the Date Picker controller to my App controller...

Paperclip not showing on the edit page

I am having some issues with paperclip, on my new page, I have a paperclip file that relates to aonther model. If I don't upload the keynote at the time of creating I can't go back and edit it on the edit page, I'm pretty sure its something todo with the fact that its related to another model. Here is my form partial <% form_for([@proj...

Text-indent is not working in safari firefox

Hi All, I m using a ruby code in select list i need 15px gap from left so i used a padding left="15px" its working fine in firefox but its not working ie and safari so i want to use text-indent ="15px" its working fine i safari and ie but not in firefox please any one help me to resolve this problem,, padding left is working fine in t...

How to comment multiple lines in ruby..??

How to comment multiple lines in ruby? ...

Is the ruby operator ||= intelligent?

I have a question regarding the ||= statement in ruby and this is of particular interest to me as I'm using it to write to memcache. What I'm wondering is, does ||= check the receiver first to see if it's set before calling that setter, or is it literally an alias to x = x || y This wouldn't really matter in the case of a normal variab...

Custom Rails Validation (Doesn't seem to be working)

Hey guys ive got the following Model for Accounts require 'net/http' require 'uri' require 'date' class Account < ActiveRecord::Base validates_presence_of :username, :password, :on => :update validate :valid_expiry_date, :on => :update def valid_expiry_date reply = Net::HTTP.get URI.parse("http://api.rapidshare.com/cgi-bin/rsapi....

Where am I going wrong? "undefined method 'application' for Sinatra:Module" Sinatra/Passenger/Apache

Hi, I'm trying to get my first Sinatra app off the ground, but am getting an error page from Passenger: undefined method `application' for Sinatra:Module Here's my Rackup file: require 'rubygems' require 'sinatra' set :env, :production disable :run require 'app' run Sinatra.application And the app itself: #!/usr/bin/env ruby re...

Broken sorting by values from embedded documents in MongoDB?

I am trying to sort the documents which I retrieve from MongoDB by a key that is in the first of several embedded documents. I am using mongod v1.4.3 with the ruby gem mongo-1.0.2. One document looks like this: >> pp MONGODB.collection('hotel_offer_groups').find_one "offers"=> [{"price"=>520.0, "rate"=>nil, "id"=>523960640,...

How can I simplify my nested sinatra routes?

I require nested subdirectories in my sinatra app, how can I simplify this repetitive code? # ------------- SUB1 -------------- get "/:theme/:sub1/?" do haml :"pages/#{params[:theme]}/#{params[:sub1]}/index" end # ------------- SUB2 -------------- get "/:theme/:sub1/:sub2/?" do haml :"pages/#{params[:theme]}/#{params[:sub1]}/#{...

Please help me with this Ruby code

Hi, I am new to Ruby and I know that I am not using the simple and powerful methods available within it. I made my code work but it has to be simple not this huge (especially I feel I am very bad at loop variables) i = 0 j = 0 loop_count = ((to_date-from_date)/(60*60*24)).to_i#diff b/w two dates in days loop_count.times do 48.time...

initializing hashes

Seems like I am frequently writing something like this... a_hash['x'] ? a_hash['x'] += ' some more text' : a_hash['x'] = 'first text' seems like there ought to be a better way, but I can't find it. ...

more ruby way of gsub from array

My goal is to let there be x so that x("? world. what ? you say...", ['hello', 'do']) returns "hello world. what do you say...". I have something that works, but seems far from the "Ruby way": def x(str, arr, rep='?') i = 0 query.gsub(rep) { i+=1; arr[i-1] } end Is there a more idiomatic way of doing this? (Let me note that speed...