ruby-on-rails

Remove temporary files after doing tests

During doing unit tests in Rails a few temp files are created (associated with model). When test is done I want to remove these files, so I have to find a way to do this no matter the test passes or not. So, what are you suggestions? ...

Avoiding nil in Rails views

I'm sure this has been asked already, but I can't find the answer. I have a Project model, which has a belongs_to relationship with my Client model. A client has a name, but a project doesn't necessarily have a client. In my view, I've got code like this: <%=h project.client && project.client.name %> because if the project doesn't h...

Webrat (web_steps.rb) not being used

When I execute the following cucumber script: Feature: Manage Customers In order to store customers As a user I want to create and manage customers Scenario Outline: Create Customer Given I am on new customer screen When I fill in Name with "Test Company" And I press "Create" Then I should see "Custome...

How can I output a calculated value using .detect in Ruby on Rails? (or alternative to .detect)

I currently have the following code: events.detect do |event| #detect does the block until the statement goes false self.event_status(event) == "no status" end What this does is output the instance of event (where events is a string of different Models that all collectively call Events) when the event_status method outputs a "no ...

RoR Achievement System - Polymorphic Association & Design Issues

I'm attempting to design an achievement system in Ruby on Rails and have run into a snag with my design/code. Attempting to use polymorphic associations: class Achievement < ActiveRecord::Base belongs_to :achievable, :polymorphic => true end class WeightAchievement < ActiveRecord::Base has_one :achievement, :as => :achievable end ...

redirect_to object record with a field beside the ID property

This is likely something easy to accomplish, but I'm having difficulty even articulating clearly, leading me to get all sorts of semi-germaine, but not quite what I'm after posts on SO when searching. I have a resource, we'll just use User as a simple to discuss use case. I desire SEO friendly URLs, so instead of ~/users/:id, we have ~...

What's the difference between "=" & "=>" and "@variable", "@@variable" and ":variable" in ruby?

Hi I know these are the basics of rails but i still don't know the full difference between = sign and => and the difference between @some_variable, @@some_variable and :some_variable in rails. Thanks. ...

RoR: undefined method `url_for' for nil:NilClass

I have a standard Rails application. When a Tip is created, I would like to create a Message for each User who is interested in that Tip. This sounds simple right? It should be... So, we start with a Tip Observer: class TipObserver < ActiveRecord::Observer def after_save(tip) # after the tip is saved, we'll create some messages...

Does Ruby On Rails 2.3.8 run on ruby 1.9.2?

Official Rails site states it supports 1.8.x versions, but did anyone try to run Rails 2.3.8 on ruby 1.9.2? ...

Ruby on Rails - Can't seem to figure out how to write/modify files on the live site

This works just fine in my dev environment (I am rewriting a css file): File.open(RAILS_ROOT + '\public\stylesheets\colors.css', 'w') do |w| w.puts 'some_text' end But when I run it in my prod environment (on Dreamhost) nothing happens - the file is not modified - nothing. What I need to be able to do is to overwrite an exist...

customize gem from github

I am trying to customize a gem called "gem_name" from github. I forked the project and cloned it to a directory. I changed s.name in the "spec = Gem::Specification.new do |s|" block in the rake file to a custom gem name "gem_name_myname". I also changed a line in the rails/init.rb file called "require gem_name" to require "gem_name_my...

How to split one level array to many arrays in Ruby 1.9.2

I have an array like this: [234, 235 , 343, 445] I want to convert it to look like this [[234],[235],[343],[445]] Is there core library function in ruby 1.9.2 could help me to do this fast? and if not is there a fast way? I did a small tests def test1 array = [] 10000000.times do array << rand(1000000) end ti...

Is there a better, SQL way to do this Ruby's Array.delete_if with a Rails.find request?

Hi, I have a Tag object (id, name) and a Tagging object (id, tag_id, type). I want to find all the tags that have a name like "kevin" and for which I can find a foreign Tagging object with type set to "people" (type can be set to people or some other tagging stuff). I tried with a complex SQL request in a Rails Tag.find method but didn...

Rendering a different Javascript file in respond_to

Hi everyone, I am stuck in an (apparently) simple problem. In my event_controller I have the i_like_it action: def i_like_it @event = Event.find(params[:id]) ... # logic respond_to do |format| format.js end end In my case "i_like_it" is called with :method => PUT (it is an Ajax call, "i_like_it.js.erb" will...

link_to new with parameter

I would like to create a link that pass a parameter, so I don't have to ask it in the form. Something like this : <%= button_to "New", new_skill_path(:controller => "skills",:action => "new", :request => "true") %> I can show that the parameter is taken… but on my view, The <%= f.text_field :request %> is not "true" ? (idem if I add...

How do I access the name of the Rails 3 application object?

I need to know the name of a programmers application from within a Rails 3 engine. ...

Ruby on Rails: Adding Facebook login to existing Authlogic login

I have an existing site that is working well with Authlogic login. I'm trying to add Facebook login as an option, using the authlogic_facebook_connect plugin, but it's not working properly. I'm following the instructions located here: http://github.com/jbasdf/authlogic_facebook_connect On my registration form, I added the Facebook con...

activerecord create_table like existing table

With Rails/ActiveRecord 2.3.8 I'd like to do: AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' ) But AR insists on adding "()" to the generated SQL even though the field list is blank since the table DDL is being cloned, thus resulting in e.g.: ActiveRecord::StatementInvalid: M...

Rails + cucumber + webrat + selnium : Periodic error on first scenario in feature

I have setup cucumber+webrat+selenium to test my rails app. This works well locally, and works about 50% of the time on my build (CI) server. The other 50% of the time, the first scenario in each feature fails with this error: Failed to start new browser session, shutdown browser and clear all session data Back trace starts with: or...

How do I stub out the flickraw library in my app's unit tests?

My Rails 2 app displays a slideshow of photos from Flickr via the flickraw library. My code works, but I'm stuck on how to properly write RSpec unit tests. I have a Slide class that encapsulates everything that my app needs from flickraw. It acts somewhat like a model object but doesn't use ActiveRecord. It doesn't do much work; it de...