Questions tagged [ruby-on-rails-3]

Ruby on Rails is an open-source web development framework written in Ruby. Ruby on Rails follows the principle of convention over configuration, freeing you from having to re-invent things to stay productive. Use this tag only for Rails 3-specific questions, and also tag those questions [ruby-on-rails].

Ruby on Rails 3.0 was a major revision of Ruby on Rails in 2010, the open-source web development framework designed to make programmers happy and productive. Rails 3.0 brings a bunch of new features and improvements over older Rails versions, including:

  • Brand new router with an emphasis on RESTful declarations
  • New Active Record chainable query language built on top of relational algebra
  • Unobtrusive JavaScript helpers with drivers for Prototype, jQuery
  • Explicit dependency management with Bundler

See Ruby on Rails 3.0 Release Notes for more information.

Multiple sub-versions have been released, namely 3.0, 3.1 and 3.2. Questions regarding specific sub-versions of Ruby on Rails 3.0 can also be asked on the appropriate tags:

Resources:

See also:

56082 questions
364
votes
7 answers

Rails: How to run `rails generate scaffold` when the model already exists?

I'm new to Rails so my current project is in a weird state. One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc. I now realize I should have generated it with rails generate…
Lan
  • 6,039
  • 7
  • 22
  • 24
335
votes
4 answers

Replace words in a string - Ruby

I have a string in Ruby: sentence = "My name is Robert" How can I replace any one word in this sentence easily without using complex code or a loop?
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52
276
votes
12 answers

Best way to load module/class from lib folder in Rails 3?

Since the latest Rails 3 release is not auto-loading modules and classes from lib anymore, what would be the best way to load them? From github: A few changes were done in this commit: Do not autoload code in *lib* for applications (now you need…
Vincent
  • 16,086
  • 18
  • 67
  • 73
266
votes
13 answers

Rails - Could not find a JavaScript runtime?

I created a new Rails project using rails 3.1.0.rc4 on my local machine but when I try to start the server I get: Could not find a JavaScript runtime. See here for a list of available runtimes. (ExecJS::RuntimeUnavailable) Note: This is not about…
255
votes
7 answers

How to make a HTTP request using Ruby on Rails?

I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response. How can I make this in Ruby on Rails? If it is possible, is it a correct…
user502052
  • 14,803
  • 30
  • 109
  • 188
248
votes
13 answers

How can I replace a hash key with another key?

I have a condition that gets a hash. hash = {"_id"=>"4de7140772f8be03da000018", .....} Yet, I want to rename the key of that hash as follows. hash = {"id"=>"4de7140772f8be03da000018", ......} P.S. I don't know what keys are in the hash; they…
Manish Das
  • 3,785
  • 2
  • 21
  • 34
247
votes
5 answers

How to restart a rails server on Heroku?

Locally I just interrupt (ctrl-c) and then start it again. How do I do the same thing with an app on heroku?
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
241
votes
7 answers

Add a CSS class to <%= f.submit %>

My question is easy: <%= f.submit %> Where does the class declaration go? I'm getting errors on multiple attempts.
sscirrus
  • 55,407
  • 41
  • 135
  • 228
241
votes
25 answers

rmagick gem install "Can't find Magick-config"

I get the error shown below when attempting to install the rmagick gem. I am on Snowleopard 10.6 using RVM, Ruby 1.9.2-head and Rails 3.05. Responses to similar questions recommended installing ImageMagick, which I successfully did. Other suggested…
Joe
  • 3,352
  • 3
  • 20
  • 19
238
votes
7 answers

ActiveRecord: size vs count

In Rails, you can find the number of records using both Model.size and Model.count. If you're dealing with more complex queries is there any advantage to using one method over the other? How are they different? For instance, I have users with…
Andrew
  • 42,517
  • 51
  • 181
  • 281
237
votes
10 answers

Rails - How to use a Helper Inside a Controller

While I realize you are supposed to use a helper inside a view, I need a helper in my controller as I'm building a JSON object to return. It goes a little like this: def xxxxx @comments = Array.new @c_comments.each do |comment| @comments…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
235
votes
5 answers

Use rvmrc or ruby-version file to set a project gemset with RVM?

I use RVM, the Ruby Version Manager to specify a Ruby version and a set of gems for each of my Rails projects. I have a .rvmrc file to automatically select a Ruby version and gemset whenever I cd into a project directory. After installing RVM…
Daniel Kehoe
  • 10,952
  • 6
  • 63
  • 82
233
votes
5 answers

Change a column type from Date to DateTime during ROR migration

I need to change my column type from date to datetime for an app I am making. I don't care about the data as its still being developed. How can I do this?
jdog
  • 10,351
  • 29
  • 90
  • 165
214
votes
14 answers

Best way to pretty print a hash

I have a large hash with nested arrays and hashes. I would like to simply print it out so it 'readable' to the user. I would like it to be sort of like to_yaml - that's pretty readable - but still too tech looking. Ultimately its going to be end…
Adam O'Connor
  • 2,582
  • 2
  • 16
  • 19
209
votes
3 answers

Understanding :source option of has_one/has_many through of Rails

Please help me in understanding the :source option of has_one/has_many :through association. The Rails API explanation makes very little sense to me. "Specifies the source association name used by has_many :through => :queries. Only use it if the…
trivektor
  • 5,608
  • 9
  • 37
  • 53