Undefined method `key?' for nil:NilClass PaperClip

Hello, I have a problem with the paperclip, I put on my GemFile

gem “paperclip”, “~> 2.6.0”

I create a migrate

class FileUpload < ActiveRecord::Migration
def up
change_table :projects do |t|
t.has_attached_file :image
end
end

def down
drop_attached_file :projects, :image
end
end

on my model I put

class Project < ActiveRecord::Base

has_attached_file :image, :styles => { :medium => “300x300>”, :thumb
=> “100x100>” }

end

and in my view I put

<%= form_for @project, :url => { :action => “create” }, :html => {
:multipart => true } do |f| %>

<%= f.label :title, "Título" %>
    <%= f.text_field :title %><br />

<%= f.label :description, "Descrição" %>
    <%= f.text_area :description %><br />

<%= f.label :image, "Imagem" %>
    <%= f.file_field :image %><br />

<%= f.submit "Criar novo Projeto" %>

<% end %>

But when I go to my action, this errors show.

NoMethodError in ProjectsController#new

undefined method `key?’ for nil:NilClass

Rails.root: /home/felipe/rails/Younner
Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Show env dump
Response

Headers:

None

Thank you.

UPDATE:

I got make it work, but now another error is showing, when I put a
image, on my file_field the rails add a div error.

I don’t know why…

On Fri, Feb 17, 2012 at 13:10, Felipe Pieretti U.
[email protected] wrote:

NoMethodError in ProjectsController#new

undefined method `key?’ for nil:NilClass

That’s telling you that there’s a problem in the one piece you didn’t
show us, the controller. Somewhere in your Projects controller,
you’re calling “key?” on something that hasn’t been properly set.

-Dave


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see www.DaveAronson.com, and blogs at
www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

in your create action why do you create the record before validation ?
should not it be ?

def create
@project = Project.new(params[:project])
if @project.save
flash[ :notice ] = “Projeto criado com sucesso !”
redirect_to :action => “index”
else
render :new
end
end

On 17 fv, 19:59, Felipe Pieretti U. [email protected]

I’m sorry Dave, I was updated the post, but

I got make it work, but now another error is showing, when I put a
image, on my file_field the rails add a div error.

I don’t know why…

Now I put the image on file field, but when I click on submit, the page
is reload to my new action, with the error div in the file field.

my controller

class ProjectsController < ApplicationController
def index
@projects = Project.all
end

def new
@project = Project.new
end

def create
@project = Project.create( params[ :project ] )

if @project.valid?
  flash[ :notice ] = "Projeto criado com sucesso !"
  redirect_to :action => "index"
else
  render "new"
end

end
end

and my action after clicked on submit:

Imagem

The same error, it look like the paperclip is not working, but I’m
following the documentation:

http://rubydoc.info/gems/paperclip/2.6.0/frames

Thank you.

hi

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs