Reputation: 32
I'm making a photo gallery using paperclip and keep getting this error
undefined method `pictures' for nil:NilClass
when creating a new picture. I have all of my associations set up and like
gallery -> has_many :pictures
picture -> belongs_to :gallery
and in my pictures.controller I have
def create
@picture = @gallery.pictures.new(picture_params)
respond_to do |format|
if @picture.save
format.html { redirect_to @picture, notice: 'Picture was successfully created.' }
format.json { render :show, status: :created, location: @picture }
else
format.html { render :new }
format.json { render json: @picture.errors, status: :unprocessable_entity }
end
end
I've spent the last day trying to figure this out.
Upvotes: 0
Views: 2629
Reputation: 239311
All we can really tell you based on what is posted is that @gallery
is nil. You actually have to assign something to @gallery
, Rails contains no boilerplate code to populate that variable for you.
Upvotes: 2