15

I just created a new app from scratch on rails4.0.0.rc1 with 1.9.3-p374 (which should work according to the guides).

However, I fail doing a simple create-action.

class Books
  def create
    @book = Book.new book_params

    if @book.save
      redirect_to @book
    else
      render action: :new
    end
  end

  private

  def book_params
    params.require(:book).permit(:name, :description)
  end
end

Doing a create here results a

undefined method `synchronize' for nil:NilClass

with the error pointing to the line of if @book.save.

Is that because of 1.9.3 or did I miss something?

pdu
  • 10,295
  • 4
  • 58
  • 95
  • can you debug or log `@book` to see that it is initialized alright? – Jakob W May 03 '13 at 18:57
  • `@book`is okay. Apparently, that only happens when the model has a uniqueness validation in it. I filed a bug (https://github.com/rails/rails/issues/10454) – pdu May 03 '13 at 20:57
  • I was wrong, the problem is when assigning an empty array to an association, like `@book.genre_ids = []`. – pdu May 03 '13 at 21:04
  • Ok, that can be useful to know =) – Jakob W May 04 '13 at 09:19
  • I ran into this error when I was attempting to do something with all model classes, but inadvertently attempted to do so with the singleton class of a model class as well. It e.g. has the method #attribute_names, but attempting to call it will run into this error. – Confusion Dec 24 '20 at 09:16

2 Answers2

35

Apparently, It was failing because of the friendlyId gem. If you use friendlyId, you may need to specify the rails4-branch.

gem 'friendly_id', github: 'FriendlyId/friendly_id', branch: 'rails4'

edit: Make sure to get the actual branch/version from norman/friendly_id. According to the readme, it needs to be at least branch: 5.0.0-beta to work with rails4.

edit2: now it is 5.0.0.rc2, as I said, make sure to get the actual branch/version, I won't obviously update this answer every time there is a version update. Also, read the comments to this answer, there are plenty of helpful information.

pdu
  • 10,295
  • 4
  • 58
  • 95
  • 1
    Problems deploying with this config; here is the gist with the error : https://gist.github.com/bbnnt/6261095 / if any evidence of why this is happening, that'd be great ! – Ben Aug 18 '13 at 10:59
  • 1
    Yup, had that also. The branch changed. Use `github: norman/friendly_id' and the according branch. Currently, there is '5.0.0.beta', which also requires some updates in your controller or model. See https://github.com/norman/friendly_id#what-changed-in-version-50 – pdu Aug 27 '13 at 06:54
  • Got it since then thx ! Now another problem; they're saying that the "FriendlyId 4-style finders" can still be used by adding the specification (somewhere in the beginngin of their "What Changed in Version 5.0" on the github page). Problem is, it does not work. At east on my side.Had to add Model.friendly.something… everywhere – Ben Aug 27 '13 at 13:53
  • same here, have a look at the issues and/or report one. – pdu Aug 27 '13 at 18:42
  • I guess they might be working on a new release as rails4 tends to be more widely used... hope so anyway; adding friendly everywhere is not that cool – Ben Aug 27 '13 at 22:03
3

pduersteler's answer didn't work for me and failed with this error:

fatal: ambiguous argument 'rails4': unknown revision or path not in the working tree.

The README was update on Sep 23, 2013 with the following, which installed without error:

gem 'friendly_id', '5.0.0.rc2' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
David
  • 469
  • 4
  • 14