ruby-on-rails

Rails Resource Restful Routes Helper functions and nil oject error

Hello I am getting an error when trying to use the resource route helper functions <%= link_to_remote "Delete", { :method => :delete, :url=> phone_numbers_url(phone_number_display.id), :update => "section_phone" }%> and in my routes i have map.resources :phone_numbers I get the following error You have a nil o...

How to parse substring between last set of parentheses in string in ruby.

In my ruby on rails app, I am trying to build a parser to extract some metadata out of a string. Let's say the sample string is: The quick red fox (frank,10) jumped over the lazy brown dog (ralph, 20). I want to extract the substring out of the last occurence of the ( ). So, I want to get "ralph, 20" no matter how many ( ) are ...

Save has_and_belongs_to_many child

I have a User model and a Role model. They are joined by a has_and_belongs_to_many relationship. When an admin creates a user I want them to be able to assign a role to the user and have it saved when I call @user.save The thing is though is that I get a warning that I can't mass-assign the roles relationship. Any suggestions on how to...

Rails pagination error

I'm using the will_paginate plugin and I get the following error only when I'm running on a server rather than locally: undefined method `total_pages' for []:Array Extracted source (around line #8): 5: <% session[:page] = params[:page] %> 6: <h2>Previous Scenario</h2> 7: <% end %> 8: <%= will_paginate @scenarios, :next_label => 'Old...

Eager loading a named_scope from an association in Rails

Is there any way to eager load a named_scope from an association? I have my Article model: class Article < ActiveRecord::Base has_many :comments end and my Comment model: class Comment < ActiveRecord::Base belongs_to :article named_scope :approved, :conditions => { :approved => true } named_scope :unapproved, :conditions =>...

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes? If I take this form: <% form_for(@thing) do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.submit 'Create' %> </p> <% end %> And alter it to improve the clar...

rails: how to html encode/escape a string? is there a built-in?

Yay, silly question time. So I have an untrusted string that I simply want to show as text in an html page. All I need to do is escape the chars '<' and '&' as html entities. The less fuss the better. I'm using utf8 and don't need no other stinking entities for accented letters and so on. Is there anything built-in in ruby or rails, ...

Rails ActiveRecord Relationships

How do the relationships magically function when only the models are altered? If I want a "has__and___belongs___to__many" relationship, what should I name the table (so Rails can use it) that contains the two foreign keys? ...

How do I use redirect_to if I've got multiple controllers in different subdirectories?

I've created a small application to learn RoR. (Book database) It consists of a read-only area and a read-write admin area. After I've got the admin functionality working first, I've moved the controller into a subdirectory and created the read-only controller. Now when I'm updating a book in the admin area, the redirect_to function re...

Rails named_scope with has_and_belongs_to_many

Hi, i have 3 tables - films, films_genres (for connect 2 tables) and genres. In Model film.rb - has_and_belongs_to_many :genres In Model genre.rb - has_and_belongs_to_many :films So, how I can write this sql code: SELECT * FROM genres INNER JOIN films_genres ON genres.id = films_genres.genre_id WHERE (films_genres.film_id = 1 ) with n...

Rails Migration

My migration is as follows: class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :email t.string :password t.string :name t.boolean :male t.boolean :admin t.timestamps end end def self.down drop_table :users end end When I go to script/con...

How do I get an installed ruby gem included in rails?

I am attempting to get a gem I've just installed working in a rails application. I can require the gem just fine in a ruby program that I run from the command line using: require 'nokogiri' But when I attempt to do the same in one of my rails controllers it errors saying "no such file to load -- nokogiri". I tried using the full pat...

Formatting timestamps

How do you format Rails timestamps in a more human-readable format? If I simply print out created_at or updated_at in my view like this: <% @created = scenario.created_at %> Then I will get: 2009-03-27 23:53:38 UTC ...

how (replace|create) an enum field on rails 2.0 migrations?

I would like to create an enum field at sone migration I'm doing, I tried searching in google but I can't find the way to do it in the migration the only thing I found was t.column :status, :enum, :limit => [:accepted, :cancelled, :pending] but looks like the above code runs only on rails 1.xxx and since I'm running rails 2.0 this...

Activerecord: Finding non associated plus associated records with a single query

Given the following scenario, using activerecord: User belongs_to Event I want to find all the Users who do not have an Event with the name "party". So I tried the following: User.all(:joins => :event, :conditions => ["events.name != ?", "party"]) However, that will only return those Users who do have an event, but just not a "part...

Lazy loading/caching of SQL query results with a model

I'm developing a system (with Rails 2.3.2, Ruby 1.8.7-p72) that has a sizable reporting component. In order to improve performance, I've created a Report model to archive old reports. The idea is that if a matching report already exists for an arbitrary set of conditions then use it, otherwise generate the report and save the results. ...

Rails Reserved Class Names

I tried creating a model called "class" (as in a graduating class of students), and encountered all kinds of problems. What are some other words or class names to avoid in Rails? Some links I've found: http://juicebar.wordpress.com/2007/05/30/reserved-words-in-rails/ http://railsforum.com/viewtopic.php?id=22242 ...

Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?

I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses open-uri to retrieve the image from S3 and MiniMagick to perform the rotation. The rotate method contains this line to retrieve the image for use with MiniMagick: temp_image = MiniMagick::Im...

Strip method for non-whitespace characters?

Is there a Ruby/Rails function that will strip a string of a certain user-defined character? For example if I wanted to strip my string of quotation marks "... text... " http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html#M000942 ...

What is the best way for having a ruby service on rails?

I need to run a ruby-script as a service. The script needs access to the ActiveRecords of a rails-app. What would be the best way? A rake task? How can it be started as a service on both windows and linux? ...