ruby

Can I limit the method in Ruby on Rails is POST method only?

For example: class UsersController < ApplicationController def doSomething end def doSomethingAgain end end Can I restrict the user pass a get method only to doSomething, but doSomethingAgain is only accept post method, can I do so? ...

English Sentence to Camel-cased method name

I had to convert a series of sentences into camel-cased method names. I ended writing something for it. I am still curious if there's something simpler for it. Given the string a = "This is a test." output thisIsATest I used for following: a.downcase.gsub(/\s\w/){|b| b[-1,1].upcase } ...

Ruby class declaration question

Can't i do it like this: class Hardware before_filter def before_filter puts "ge" end end It says before_filter is undefined method or variable when I instantiate it hd = Hardware.new because ive seen others put a method name in a class before. Just wondering how it works. Thanks ...

Passing a variable into a models after_initialize method

I have the following (heavily simplified) model, that uses will_paginate class Search < ActiveRecord::Base attr_reader :articles def after_initialize @articles = Article.paginate_by_name name, :page => 1 end end and the controller code in my show action is @search = Search.new(params[:search]) This all works fine, but ...

Metric Fu: RCov fails to load spec_helper

I am trying to run metric_fu on a Rails 3 application. All is good, with the exception of rcov. I have RSpec configured and my tests follow the spec/*/.rb format and run fine in RSpec. Trying to check coverage with rcov, however, I get the following errors: ** Running the specs/tests in the [test] environment No file to analyze was ...

Ruby on Rails: how do I set a variable where the variable being changed can change?

i want to do current_user.allow_????? = true where ????? could be whatever I wanted it to be I've seen it done before.. just don't remember where, or what the thing is called. ...

Ruby on Rails: How do I set a variable to a constant, where part of the name of the constant can change?

I want to do current_user.field = User::????????? Where ?????????? would be whatever I wanted it to be This what I'm trying to do Given /^"([^\"]*)" is a(?:|n) "([^\"]*)"$/ do |arg1, arg2| cur_user = User.find(:first, :conditions => ['name = ?', arg1]) cur_user.update_attributes(:role => User::arg2.constantize) end While const...

Ruby - update class attributes hash when a property changes

Hi, I'm trying to write a ruby class that works similarly to rails activerecord model in the way that attributes are handled: class Person attr_accessor :name, :age # init with Person.new(:name => 'John', :age => 30) def initialize(attributes={}) attributes.each { |key, val| send("#{key}=", val) if respond_to?("#{key}=") } ...

servers with ruby

Hi, I was doing the following on the terminal: script/server it says the server host:3000 was being used, when I typed it into web browser, the page loads but when I click on my environment the info doesn't show up. I get an error message if I change the server by typing in script/server -p 3001 for example, it works, so my qu...

Parallel uploading to Amazon S3 using Ruby and amazon.rubyforge.org

Hi, I am using Amazon S3 service to upload different directories (and the files inside) to different buckets (directory -> bucket). I am coding in Ruby, and I am using the lib http://amazon.rubyforge.org. The files are small (about 20KB). I'd like to upload the directories in parallel (using many threads) but I have to use synchronize ...

How to generate a radio button form in a form_for block?

I know how to loop a combo box by using this: <%= f.collection_select(:category_id , Category.all, :id, :name, {:prompt => false})%> and now, I can do radio box using the radio box help: radio_button("user", "receive_newsletter", "yes") But how can I do this in a form helper? thank you. ...

Rails Console: Why does it stop working after backgrounding it?

Hey guys. I'm able to send Rails consoles to the background with CTRL+Z . However, when I bring back the console with "fg", the console's no longer responsive. It doesn't respond to normal commands, or even "quit" or CTRL+C. I have to background it again with CTRL+Z , and then kill it with "kill %1". Here's an example: [nickh@chameleo...

Buffered socket reading

Hi guys, I have a problem - I don't know the amount of data being sent to my UDP server. The current code is this - testing in irb: require 'sockets' sock = UDPSocket.new sock.bind('0.0.0.0',41588) sock.read # Returns nothing sock.recvfrom(1024) # Requires length of data to be read - I don't know this I could set recvfrom to 65535 ...

Ruby require 'file' and relative location...

So I'm writing some rspec tests and I'm embarrassed at my lack of Ruby understanding. I have a file structure that looks like the following: GUI_Tests/Tests/test_spec.rb GUI_Tests/windows_gui.rb GUI_Tests/upload_tool.rb when I run spec for the test_spec.rb file, I require the upload_tool file to be included like so: spec -r ../...

Why is this instance variable nil when accessed in a different unit test?

require 'rubygems' require 'test/unit' class Thing attr_accessor :foo def set_stuff @foo = 'bar' end end class ThingTest < Test::Unit::TestCase def setup @thing = Thing.new end def test_set_stuff @thing.set_stuff assert 'bar' == @thing.foo end def test_foo_in_other_test puts @thing.foo assert ...

Ruby Get Available Disk Drives

Can anyone tell me how I can get a list of the available disk drives in ruby? I am creating an open file dialogue and need to know! Thanks in advance, ell. ...

Convert a SQL Query to Rails

At the moment, I am doing my complex queries by hand so to speak. But I keep encountering problems. For instance. query = "SELECT histories.candidate_id FROM histories WHERE histories.institution_id IN (?) GROUP BY histories.candidate_id HAVING COUNT(*)= ?" cand = [Code.fi...

NoMethodError (set_result) in SQLite3 when running Geocommons Geocoder using irb on Mac OS X 10.5.8

Has anyone here ever encountered the following error after installing and attempting to run the Geocommons geocoder on Mac OS X 10.5.8? This is my exact output from the Terminal window from the point at which I started irb: $ irb >> require 'geocoder/us' => true >> db = Geocoder::US::Database.new("/opt/tiger/orangeca.db") => #<Geocoder...

Access existing MySql database with ruby (paths?)

Hello! I have an existing mysql db used by a rails app. I want to create a Ruby script to run to perform a weekly maintenance task on the database. My first line is: require 'mysql' I have tried various 'paths' to point there but get an error message like: 1:in `require': no such file to load I have: code at: \ruby\sites\som...

Callback after delayed_job process job

I need to update a model after delayed_job processes a task on it, e.g: foo.delay.something After something is done, I need to update the foo object, what is the best way to achieve this? I was thinking in coding a callback on the Delayed::Backend::ActiveRecord::Job class, but there's should be something cleaner and better to do this. ...