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
42
votes
7 answers

warning: toplevel constant referenced

I have four models (Document, Question, Question::Document, and Answer). In my Answer model I have validates :text, presence: { :unless => Proc.new{ |a| a.question.is_a? Question::Document } } This gives me the the warning warning: toplevel…
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
41
votes
11 answers

Ruby on Rails 4: Pluck results to hash

How can I turn: Person.all.pluck(:id, :name) to [{id: 1, name: 'joe'}, {id: 2, name: 'martin'}] without having to .map every value (since when I add or remove from the .pluck I have to do he same with the .map)
Lucas Steffen
  • 1,244
  • 2
  • 10
  • 22
41
votes
1 answer

Rspec/Capybara loading in progress, circular require considered harmful

Unsure if this is a Capybara issue, but I just did a fresh install of Rails (4.1.1) and Rspec Rails (3.0.1) and I would like to use Capybara (2.3.0), but having never used it I'm running into issues. I created a "spec" in spec/features and the spec…
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
41
votes
1 answer

ArgumentError (too few arguments): when calling format.json on rails 4.04

When executing format.json{render json: {}, status: :ok} in Rails 4.0.4, I get the following error: ArgumentError (too few arguments): Although I have another program (with Rails 3.2.13) where the exact same line executes with no problem. am I…
juan Isaza
  • 3,646
  • 3
  • 31
  • 37
41
votes
5 answers

How to get blank checkboxes to pass as false to params

I have a form that is an update user form where several of the elements are checkboxes. I want true to pass to the params if checked (this is working) and false to pass to the params if unchecked (not working). The unchecked items are not even…
brad
  • 1,675
  • 2
  • 16
  • 23
40
votes
6 answers

Ruby: substring to a certain length and also to last whitespace within substring

I am trying to truncate a long string of text to a certain length, but want to also make sure that the truncated result ends at a whitespace. I am also going to append an ellipsis afterwards. For example this: "This is a very long string that has…
e_r
  • 794
  • 2
  • 8
  • 18
40
votes
2 answers

Rails has_one / has_many, dependent option: What values are available?

I get the following error in Rails 4: dependent option must be one of destroy delete Per https://github.com/rails/rails/issues/3458, other options were supported in older versions. But what is possible nowadays? I could not find any other…
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
40
votes
5 answers

Mongoid Rails 4 sort by asc or desc order created_at

I have a rails 4 app using Mongoid. I want to do something basic is display the book model I have by descending order according to the field created_at in the index view. In the controller books_controller.rb: def index @books =…
Chleo
  • 935
  • 2
  • 8
  • 14
40
votes
6 answers

Upgrading to devise 3.1 => getting Reset password token is invalid

Solution Thanks to this gist form Steven Harman, I got it working. devise_mail_helpers.rb module Features module MailHelpers def last_email ActionMailer::Base.deliveries[0] end # Can be used like: # …
Andreas Lyngstad
  • 4,887
  • 2
  • 36
  • 69
39
votes
5 answers

Test ActiveModel::Serializer classes with Rspec

Given the following ActiveModel::Serializer class: class SampleSerializer < ActiveModel::Serializer attributes :id, :name end How can this be tested with RSpec?
gnerkus
  • 11,357
  • 6
  • 47
  • 71
39
votes
9 answers

Rails: Render view from outside controller

I'm trying to create an HTML string using a view. I would like to render this from a class that is not a controller. How can I use the rails rendering engine outside a controller? Similar to how ActionMailer does? Thanks!
prajo
  • 737
  • 2
  • 8
  • 17
39
votes
7 answers

Rails, Devise, Rspec: Undefined method 'sign_in'

I am trying to write Rspec tests in Rails, using Devise helper methods for signing in and out. The sign_in method is not working. However, it had been working earlier, before a slew of changes to the app. Things I have tried: I am including the…
steel
  • 11,883
  • 7
  • 72
  • 109
39
votes
2 answers

Best way to go about sanitizing user input in rails

I've read a lot about this and know there are many related questions on here, but I couldn't find a definitive guide for how to go about sanitizing everything. One option is to sanitize on insert, for example I have the following in my…
Dave
  • 1,051
  • 1
  • 10
  • 20
39
votes
2 answers

GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function

I have code in my controller that is ranking albums by the highest average review rating (used code from this solution How to display highest rated albums through a has_many reviews relationship): @albums = Album.joins(:reviews).select("*,…
Reuben
  • 701
  • 1
  • 9
  • 17
39
votes
4 answers

Add Custom Field/Column to Devise with Rails 4

I'm attempting to add a full_name field/column to my User model (using the devise gem) and Rails 4. Most of the examples online recommend using attr_accessible, but it sounds like this should be approached differently in Rails 4. How would I add…
Neil Kelty
  • 619
  • 1
  • 6
  • 9