Eduardo Almeida
Eduardo Almeida

Reputation: 410

Rails - undefined method `'

Rails is throwing a

"*** NoMethodError Exception: undefined method `' for #<Marketplace::Item:0x000001019dd148>" 

After a .save or .valid? on @item (example below), I don't see where is the problem, from my Rails knowledge I would say that a column or a method were not setup correctly, but the model is pretty straight and there not much in the controller as well, could my .valid? getting lost due some attribute/param?

item.rb:

class Marketplace::Item < ActiveRecord::Base
include Shared::AttachmentHelper 

extend FriendlyId 
belongs_to :musico
has_many :anuncios
has_many :item_images, :dependent => :destroy

#Paperclip
has_attachment :cover,
    :styles => { 
        :large => "741x570>", 
        :medium => "470x611>", 
        :thumb => "235x",
        :thumb_anuncio => "90x90#"  
    },
    :convert_options =>{
        :large => ["-strip","-quality 80"],
        :medium => ["-strip","-quality 80"],
        :thumb => ["-strip","-quality 80"]
    },
   :default_url => "missing-image.png" 

validates_attachment :cover,
    :content_type => { :content_type => ['image/jpeg', 'image/png', 'image/jpg', 'image/bmp'] },
    :size => { :in => 0..8.megabytes }

#Opcoes do DropDown de STATUS
STATUS = [  "ATIVO", 
            "INATIVO"]

end

items_controller.rb

def create
    @item = Marketplace::Item.new(item_params)

    respond_to do |format|
        byebug
        if @item.save
            if params[:images]
                params[:images].each { |image|
                    @item.item_images.create(image: image)
                }
            end

            format.html { redirect_to @item, notice: "Item criado." }
            format.json { render json: @item, status: :created, location: @item }
        else
            format.html { render action: "new" }
            format.json { render json: @item.errors, status: :unprocessable_entity }
        end
    end
end

Backtrace: http://pastebin.com/GrDywzKR

Upvotes: 2

Views: 2118

Answers (1)

Eduardo Almeida
Eduardo Almeida

Reputation: 410

Posting the comment:

@Max is right, looks like friendly_id ends up doing a send(something_that_is_nil.to_s) while trying to build the "friendly ID" for your model. The lack of a friendly_id :some_column call in your model and some poor argument specification/parsing and error handling on friendly_id's part would explain the error you're seeing.

Friendly it was, indeed, the problem, I was missing an include.

Upvotes: 4

Related Questions