ruby,ruby-on-rails-3,activerecordRelated issues-Collection of common programming errors
the Tin Man
ruby-on-rails ruby ruby-on-rails-3 nokogiri rails-console
I’m using Nokogiri in Rails 3.2.11 to do some screen scraping. I have this one very long method that when I copy and paste line by line into the rails console it runs flawlessly, but when I call it in the console form my model file, it breaks, sending this error: Nokogiri::CSS::SyntaxError: unexpected ‘$’ after ”Here’s the method (it’s 2, the first calls the second). Any help at all would be much appreciated:def self.recurse_craigs(add = ‘o’)years = “#{MIN_YR}%7C05%7C#{MIN_YR+1}%7C06%7C#{MIN_YR
Justin Ko
ruby watir-webdriver fieldset page-object-gem
I’m trying to access a select_list within a fieldset though Cheezy’s pageobject.The total html is far too long to post (well over 200 lines for just the fieldset), but I can supply the lines with all of the id’s and such.fieldset:<fieldset class=”dartPayer-Insurance” style=”width: 730px;”>select_list:<select id=”dartPayer-Payer” style=”width: 235px;”>Line in the pageobject I am attempting to use:select_list(:payer_insurance){ element(:class => “dartPayer-Insurance”).select_list_el
Tom De Leu
ruby metaprogramming method-missing
I’m learning metaprogramming in Ruby and am just trying out defining missing methods via method_missing and define_method. I’m getting some unexpected behaviour and am wondering if anyone can explain this. Here is my class:class Xdef method_missing(m, *args, &block)puts “method #{m} not found. Defining it.”self.class.send :define_method, m doputs “hi from method #{m}”endputs “defined method #{m}”end endNow, this code:x = X.newx.some_method puts x.some_method puts puts xProduces the output:
kiamlaluno
ruby hash
I frequently write something like this:a_hash[‘x’] ? a_hash[‘x’] += ‘ some more text’ : a_hash[‘x’] = ‘first text’There ought to be a better way to do this, but I can’t find it.
enricostn
ruby-on-rails ruby testing rspec cancan
here is the Controller I want to test:class UsersController < ApplicationControllerload_and_authorize_resourcedef index@users = User.allrespond_to do |format|format.html # index.html.erbformat.json { render json: @users }endenddef show@user = User.find(params[:id])respond_to do |format|format.html # show.html.erbformat.json { render json: @user }endend#other actions hereendAs you can see I use CanCan method load_and_authorize_resource so I’ve written a ControllerHelper for RSpec:# spec/suppor
Jason Swett
ruby time
The default time zone offset in Ruby is apparently -0800. I want to set mine to -0500. How do I do this?
the Tin Man
ruby-on-rails ruby osx terminal
I previously used a package installer from railsinstaller.org to set up an installation of Rails on my Mac OS X system. There’s always been the odd problem, but nothing major. I decided to delete it completely and start again, but now I’m seeing more problems.Is it worth completely deleting my installations of Git, Homebrew, Ruby and anything else that you might recommend, and just starting again perhaps?Here’s the problem I’m getting when typing into terminal:$ rvm reinstall 1.9.3device-3ebf56
the Tin Man
ruby-on-rails ruby exception open-uri
I have a rake task that is responsible for doing batch processing on millions of URLs. Because this process takes so long I sometimes find that URLs I’m trying to process are no longer valid — 404s, site’s down, whatever.When I initially wrote this there was basically just one site that would continually go down while processing so my solution was to use open-uri, rescue any exceptions produced, wait a bit, and then retry.This worked fine when the dataset was smaller but now so much time goes
jeremy collyer
ruby-on-rails ruby syntax-error
i having an issue with aquarium syntax for a rails app. its an app that i cloned from a github repository that i pushed it to, and it was working fine. im the only one pushing updates========================this is the error i get when i try to run rails s/Users/jcollyer/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:239:in require’: /Users/jcollyer/.rvm/gems/ruby-1.9.3-p327/gems/aquarium-0.3.0/lib/aquarium/aspects/exclusion_handler.rb:34: syntax error, un
Brian Campbell
ruby
I’m a beginner in Ruby, so I’m sorry to ask something so simple, but is there anything wrong with this code – 3.upto(9) {print “Hello”puts ” World” }or3.upto(9) { |n|print “Hello “puts n }It works well enough, but most of the code samples I see use the syntax of 3.upto(9) do |n|print “Hello “puts n endis it just the convention to only use curly braces for single statements? Coming from C/C# the first seems more natural to me, but when in Rome!
Frexuz
ruby-on-rails-3 ubuntu virtualbox
I’m currently on a Windows PC with a Ubuntu VM where I run my local Rails server. The issue is, that I have to stop and start the Rails server each time I resume the VM.Steps to produce problem:Start VM (resume state) Start Rails server Browse the Rails site on Windows (Everything OK so far) Shut down/Pause VM (save state) Resume VM Browsing the site again causes this errorError: log writing failed. Protocol error – /var/www/episodecalendar.com/log/development.log log writing failed. Protocol er
the Tin Man
ruby-on-rails ruby ruby-on-rails-3 nokogiri rails-console
I’m using Nokogiri in Rails 3.2.11 to do some screen scraping. I have this one very long method that when I copy and paste line by line into the rails console it runs flawlessly, but when I call it in the console form my model file, it breaks, sending this error: Nokogiri::CSS::SyntaxError: unexpected ‘$’ after ”Here’s the method (it’s 2, the first calls the second). Any help at all would be much appreciated:def self.recurse_craigs(add = ‘o’)years = “#{MIN_YR}%7C05%7C#{MIN_YR+1}%7C06%7C#{MIN_YR
Gazler
ruby-on-rails ruby-on-rails-3 gem usps
I’ve found the shipping gem for ruby here http://shipping.rubyforge.org/ searching on google for some information on integrating USPS API on a Ruby on rails application but when I install the gem and add it to my Gemfile as i run the bundle install whenever a try to run my application or a rake taks i get the following error:/Users/myusername/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in `require’: /Users/myusername/.rvm/gems/ruby-1.9.2-p290/gems/s
robintw
ruby-on-rails ruby-on-rails-3
I have a rails application that is giving a 500 Internal Server Error page when I try and visit it (it’s at http://www.helendan.rtwilson.com), however I can’t find any details on the error in any of the log files.I’ve tried looking inside ./log in the rails application directory and there is nothing in any of the log files there. I’ve also tried looking at the web server logs themselves, and the access.log file shows an access to / and a request for the favicon, but nothing else, and the error.l
kyu1012
ruby-on-rails-3 tdd rspec-rails nameerror
I am working through Hartl’s Ruby on Rails tutorial and am stuck on Section 3.2 where I am required to run a TDD using the command:$bundle exec rspec/requests/static_pages_spec.rband I get this in return:/home/Kelvin_Yu/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `require’: /home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:2: syntax error, unexpected ‘.’ (SyntaxError) /home/Kelvin_Yu/rails_projects/sample_app/spec/spec_helper.rb:6: syntax error, unexpected ‘.’fr
LondonGuy
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 rubygems
I’m having a hard time trying to track down an error. I’m using a separate update method for changing my users passwords when their logged in. I’ve posted the code below.In users_controller:def change_password@user = current_user@user_password_matches = User.authenticate(current_user.email, params[:current_password])if @user_password_matches.update_attributes(params[:user])login @userformat.js { render :js => “window.location = ‘#{settings_account_path}'” } flash[:success] = “Password updat
Nick ONeill
ruby-on-rails ruby ruby-on-rails-3 rspec
Not quite sure what’s going on here. I’m moving over some code from another project of mine and suddenly the same specs from before are generating errors in the new project. All the errors appear to revolve around calling the stub method. Here’s an example test:it “retrieves active workers from Redis” [email protected]_receive(:monitor_running?).and_return(false)REDIS.should_receive( :smembers ).with( ‘leaderboard-workers’ ).and_return( [] )@monitor.perform endThis works. However if I switch
Alexis
ruby-on-rails ruby-on-rails-3 internationalization
According to ruby docs, the translate (or t) helper delegates to I18n#translate but also performs several additional functions, among which is: “it’ll mark the translation as safe HTML if the key has the suffix _html”.I would expect that it should work equally in both views and in controllers, but in my experience that doesn’t so: t(:hello_html) does work as expected in views (marks the translation as html_safe), but it does not mark the result as safe html when invoked from a controller.To repr
jdartland
ruby-on-rails ruby ruby-on-rails-3 twitter-bootstrap coding-style
I’ve been stuck for a time now with my commenting system on my blog. I now need you help to solv the problem and push me in the right direction. I’m using Ruby on rails and the twitter open source framework bootstrap.show.html.erb:<h2>Comments</h2><div id=”comments”><%= render :partial => @post.comments %></div><%= form_for {@post, Comment.new} do |f| %><p><%= f.label :body, “New comment” %><br/><%= f.text_area :body %></p><
Will
ruby-on-rails ruby-on-rails-3
Found a similar question ( Ruby on Rails Tutorial Test Not Passing ) , but its answer did not fit my issue. Tried to include as much relevant information as I could. Rspec error:Failures:1) SessionsController DELETE ‘destroy’ should sign a user outFailure/Error: controller.should_not be_signed_inexpected signed_in? to return false, got true# ./spec/controllers/sessions_controller_spec.rb:58:in `block (3 levels) in ‘The test:describe “DELETE ‘destroy'” doit “should sign a user out” dotest_sign_in
Orion Edwards
Joel Grannas
ruby-on-rails ruby ruby-on-rails-3 activerecord
I have a model with an has_many association: Charts has_many ChartConditionscharts model has fields for:name (title) table_name (model)The chart_conditions model has fields forassoc_name (to .joins) name (column) value (value) operator (operatorBasically my Chart tells us which model (using the table_name field) I want to run a dynamic query on. Then the chart_conditions for the Chart will tell us which fields in that model to sort on.So In my models that will be queried, i need to dynamically
JavaFan
ruby-on-rails ruby unit-testing activerecord fabrication-gem
I am using Fabrication gem v-2.5.0 on Rails 2.3.16 but get the following errors when I run the unit test cases:Below is the code snippet :First caseFabricate(:some_modal) Fabrication::MisplacedFabricateError: # from /Users/user_xyz/.rvm/gems/ree-1.8.7- 2011.03@project/gems/fabrication-2.5.0/lib/fabrication.rb:51:in `Fabricate’ from (irb):3Second caseFabricate(:some_other_modal) SyntaxError: /Users/user_xyz/.rvm/gems/ree-1.8.7-2011.03@project/gems/fabrication-2.5.0/lib/fabrication/generat
Texas
ruby-on-rails ruby-on-rails-3 activerecord passenger
I previously posted a question (Performing multiple queries on the same model efficiently) which fellow StackExchange users kindly answered and saved me lots of processing time, avoiding lots of queries on my model by storing the results of a SQL query using the .all functionSpecifically they advised @chosenrecords = Everything.where(‘name LIKE ?’, ‘What I want’).order(‘price ASC’).all be usedThis appeared to work fantastically and I used it in a number of places in our code, however, having jus
Victor Bruno
ruby-on-rails ruby-on-rails-3 activerecord
How do I define a model attribute as an expression of another attribute?Example:Class Home < ActiveRecord::Baseattr_accessible :address, :phone_numberNow I want to be able to return an attribute like :area_code, which would be an sql expression like “substr(phone_number, 1,3)”.I also want to be able to use the expression / attribute in a group by query for a report.This seems to perform the query, but does not return an object with named attributes, so how do I use it in a view?Rails Console:
greg
ruby-on-rails ruby-on-rails-3 activerecord
I would like to create complex rest object instances with a single rest call using rails. In the example case below I get an error in the controller when I call new on Person with a parameter hash.I get an error for unexpected type when seeing a ActiveSupport::HashWithIndifferentAccess and not a PhoneNumberThe hash passed from the test contains an array of Hash objects, while the controller action parameters create ActiveSupport::HashWithIndifferentAccess objects. Any suggestions to fix the err
Mario
ruby-on-rails ruby activerecord associations
When working with activerecord I notice that while constructing associations, the reciprocal association is provided automatically only if the reciprocal model has already been saved. If it has not yet been saved, this reciprocal association must be defined manually. I am wondering if this is intentional or a bug, since the only difference in it working or failing is whether the reciprocal model has been saved or not.I hopefully clarified this with the three scenarios described in the followin
JW8
ruby-on-rails ruby-on-rails-3 activerecord
I have a legacy table with (horrible) column names like LYEAR#2, LYEAR#3 … LYEAR#9I setup a model for it named GlamasIf I have: accounts = Glamas.all account_mains = accounts.collect(&:ACCOUNT_MAIN)It breaks with this error:SyntaxError: compile error /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:273: syntax error, unexpected kUNDEF, expecting ‘)’undef :LYEAR#9?^ /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:274: sy
Norto23
ruby-on-rails-3 activerecord
Moving from Datamapper to ActiveRecord recently and one cool feature that we were using is contextual validations and using with_options that could pass an array as below:with_options when: [:started, :completed] do |v|v.validates_with_method :has_data, method: :check_data_started? endWhich would then fire all validations within the block if .valid?(:started) or .valid?(:completed) were called.Is there a way to do this with Active Record, I tried:with_options on: [:started, :completed] do |v|v.v
Pravin Mali
ruby activerecord
I am trying to connect to Oracle database using ‘activerecord’ and Ruby 1.8.7 and getting below error on my Windows 7 machine. I searched around for this issue and came across 1.8 and 1.9 ruby declaration of ‘Hash’ however I am using Ruby 1.8.7 and I feel I am using correct hash declaration, please correct me if I am wrong.C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in `gem_original_require’: C:/Ruby187/lib/ruby/gems/1.8/gems/activerecord-oracle_enhanced
Web site is in building