36,684 questions
840 votes
18 answers
588k views
How to get a random number in Ruby
How do I generate a random number between 0 and n?
431 votes
20 answers
180k views
Rails 4: how to use $(document).ready() with turbo-links
I ran into an issue in my Rails 4 app while trying to organize JS files "the rails way". They were previously scattered across different views. I organized them into separate files and compile them ...
150 votes
29 answers
460k views
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue: PG Error could not connect to server:...
647 votes
6 answers
249k views
How to use concerns in Rails 4
The default Rails 4 project generator now creates the directory "concerns" under controllers and models. I have found some explanations about how to use routing concerns, but nothing about controllers ...
371 votes
8 answers
415k views
Add a reference column migration in Rails 4
A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like? Here is what I have. I'm not sure if I should use (1) :user_id, :...
218 votes
19 answers
174k views
Rails: How to reference images in CSS within Rails 4
There's a strange issue with Rails 4 on Heroku. When images are compiled they have hashes added to them, yet the reference to those files from within CSS don't have the proper name adjusted. Here's ...
440 votes
5 answers
350k views
Rails 4: List of available datatypes
Where can I find a list of data types that can be used in Ruby on Rails 4? Such as text string integer float date I keep learning about new ones and I'd love to have a list I could easily ...
110 votes
21 answers
50k views
Error when trying to install app with mysql2 gem
Im trying to install an open source rails 3.2.21 application that uses the mysql2 gem, but when i try and run the bundle commant I get the following error: Fetching: mysql2-0.3.18.gem (100%) Building ...
155 votes
20 answers
303k views
cannot load such file -- bundler/setup (LoadError)
I'm setting Rails 4 application with Ruby 2.0, but I'm getting "Web application could not be started" and get this trace: cannot load such file -- bundler/setup (LoadError) /usr/local/lib/ruby/...
360 votes
6 answers
179k views
Rails 4: before_filter vs. before_action
In rails >4.0.0 generators creates CRUD operations with before_action not before_filter. It seems to do the same thing. So what's the difference between these two?
427 votes
4 answers
182k views
Rails update_attributes without save?
Is there an alternative to update_attributes that does not save the record? So I could do something like: @car = Car.new(:make => 'GMC') #other processing @car.update_attributes(:model => '...
191 votes
15 answers
202k views
How to solve error "Missing `secret_key_base` for 'production' environment" (Rails 4.1)
I created a Rails application, using Rails 4.1, from scratch and I am facing a strange problem that I am not able to solve. Every time I try to deploy my application on Heroku I get an error 500: ...
89 votes
17 answers
90k views
An error occurred while installing pg (0.17.1), and Bundler cannot continue
I just installed Rails 4.0.2 and when creating a new app, in the bundle stage I get: Installing pg (0.17.1) Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /Users/...
211 votes
3 answers
115k views
Rails: validate uniqueness of two columns (together)
I have a Release model with medium and country columns (among others). There should not be releases that share identical medium/country combinations. How would I write this as a rails validation?
390 votes
5 answers
64k views
Rails I18n validation deprecation warning
I just updated to rails 4.0.2 and I'm getting this warning: [deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you ...