Questions tagged [ruby-on-rails-4]

For issues specific to version 4 of Rails. If your question applies to Ruby on Rails in general, use the tag [ruby-on-rails].

Ruby on Rails 4.0, "an open-source web framework that's optimized for programmer happiness and sustainable productivity." Rails 4.0 brings a bunch of new features and improvements over older Rails versions.

See Ruby on Rails 4.0 Release Notes for more information.

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

Resources

Related tags

36770 questions
825
votes
18 answers

How to get a random number in Ruby

How do I generate a random number between 0 and n?
Mark A. Nicolosi
  • 82,413
  • 11
  • 44
  • 46
645
votes
7 answers

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 or models. I am pretty sure it has to do with…
yagooar
  • 15,959
  • 6
  • 20
  • 21
438
votes
5 answers

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 refer to.
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
430
votes
20 answers

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 with the assets pipeline. However, I just learned…
emersonthis
  • 32,822
  • 59
  • 210
  • 375
421
votes
4 answers

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 => 'Sierra', :year => "2012", :looks => "Super Sexy, wanna make…
tybro0103
  • 48,327
  • 33
  • 144
  • 170
390
votes
5 answers

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 can set I18n.enforce_available_locales = false to…
Mauricio Moraes
  • 7,255
  • 5
  • 39
  • 59
368
votes
8 answers

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, :int or (2) :user, :references. I'm not even sure if…
360
votes
6 answers

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?
freemanoid
  • 14,592
  • 6
  • 54
  • 77
265
votes
6 answers

How is attr_accessible used in Rails 4?

attr_accessible seems to no longer work within my model. What is the way to allow mass assignment in Rails 4?
user2532974
  • 2,777
  • 3
  • 13
  • 6
232
votes
4 answers

Auto-loading lib files in Rails 4

I use the following line in an initializer to autoload code in my /lib directory during development: config/initializers/custom.rb: RELOAD_LIBS = Dir[Rails.root + 'lib/**/*.rb'] if Rails.env.development? (from Rails 3 Quicktip: Auto reload lib…
Yarin
  • 173,523
  • 149
  • 402
  • 512
230
votes
5 answers

Paperclip::Errors::MissingRequiredValidatorError with Rails 4

I'm getting this error when I try to upload using paperclip with my rails blogging app. Not sure what it is referring to when it says "MissingRequiredValidatorError" I thought that by updating post_params and giving it :image it would be fine, as…
nadia
  • 2,519
  • 5
  • 15
  • 12
215
votes
17 answers

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 what I mean. I have a file called logo.png. Yet…
Nick ONeill
  • 7,341
  • 10
  • 47
  • 61
207
votes
7 answers

Determine what attributes were changed in Rails after_save callback?

I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful before the model is saved, the way I'm currently…
modulaaron
  • 2,856
  • 3
  • 24
  • 28
203
votes
3 answers

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?
200
votes
13 answers

Rails 4 Authenticity Token

I was working on a new Rails 4 app (on Ruby 2.0.0-p0) when I ran into some authenticity token problems. While writing a controller that responds to json (using the respond_to class method), I got to the create action I started getting…
alexcoco
  • 6,657
  • 6
  • 27
  • 39
1
2 3
99 100