Reputation: 3
Insert obligatory "I'm a Rails newbie" comment here.
TL;DR: Cannot .create or .new instance of a model
My basic structure is that Users are supposed to be able to have multiple notes which will have attachments handled by the "paperclip" gem. However, I can't seem to be able to actually create a note:
undefined method `[]' for nil:NilClass
def create
@user = User.find(params[:user_id])
@note = @user.notes.create(params[:note].permit(:topic, :class, :content))
redirect_to user_path(@user)
end
This is coming from the Note Controller and the error occurs in the line where I use @user.notes.create. What I've boiled the problem down to is that notes is empty at the beginning and for some reason the "create" call is failing on the empty notes array. Interestingly enough, Note.new also fails with the same error if I just try to instantiate an empty note to work with.
Models for reference:
class Note < ActiveRecord::Base
belongs_to :user
has_attached_file :content,
:storage => 's3',
:bucket => ENV['AWS_BUCKET'],
:path => "uploads/:attachment/:id/:styles.:extension",
:styles => {
:medium => "300x300>",
:thumb => "100x100>" },
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'] },
:s3_permissions => 'public-read'
end
class User < ActiveRecord::Base
has_many :notes
validates_presence_of :username
validates_presence_of :email
validates_presence_of :school
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9] {2,4}\z/i
end
Let me know if any more info is needed, first time Rails user, first time Stack Exchange poster :)
Upvotes: 0
Views: 709
Reputation: 124469
My best guess is that your class
column is causing major problems. I would generally treat class
as a "reserved word" in Rails and never use it for a column name. (When I just tried creating a test model with a class
column, I couldn't even get the form for it to load without getting a stack level too deep
error).
Upvotes: 3