Search code examples
ruby-on-railsruby-on-rails-4herokunomethoderrorgriddler

NoMethodError (undefined method `posts' for nil:NilClass)


I'm very new to rails and was using the gem griddler to hopefully parse some incoming emails. I used all the code from the github site, and it provides an example emailprocessor.rb here:

class EmailProcessor
  def initialize(email)
    @email = email
  end

  def process
    //re-done with help from @Kris
    user = User.find_or_create_by(email: @email.from[:email]) do |u|
      u.email = @email.from[:email]
      u.name = @email.from[:name]
    end    


    user.posts.create!(
      subject: @email.subject,
      body: @email.body,

      attachment: @email.attachments,
      from: @email.from[:email]
    )
  end
end

I know i need to declare posts somehow and somewhere, but I have looked around on here and on the rails site and havent found anything to help me create it. I get the error in the title when running heroku logs -t when the email server tries to post to the page.

here are the rest of my files that may be needed and what I've tried:

User.rb:

class User < ActiveRecord::Base
    has_many :posts
end

Post.rb:

class Post < ActiveRecord::Base
    belongs_to :user
end

create_posts.rb

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :subject
      t.string :body
      t.string :from
      t.string :attachment

      t.timestamps
    end
  end
end

users_controller.rb

class UserController < ApplicationController
  def list
    @users = User.find(:all)
  end

  def index
    @users = User.all
  end

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def edit
  end

  def create
    @user = User.new(params[:user])
    if @user.save
        redirect_to :action => 'list'
    else
        render :action => 'new'
    end
  end
end

post_controller.rb

class PostController < ApplicationController


 def list
    @posts = Post.find(:all)
  end

  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def edit
  end

  def create
    @post = Post.new(params[:post])
    if @post.save
        redirect_to :action => 'list'
    else
        @post = Subject.find(:all)
        render :action => 'new'
    end
  end

end

Any help is really appreciated, I'd like to know what I'm doing wrong so I can avoid it in the future


Solution

  • The e-mail address is not found in the database and therefor user is nil. You can create a user on the fly with the following code:

    def process
      user = User.find_or_create_by(email: @email.from[:email]) do |u|
        # todo: set some other properties on the new user u, e.g. the name
      end
    
      user.posts.create!(
        subject: @email.subject,
        body: @email.body,      
        attachment: @email.attachments,
        from: @email.from[:email]
      )
    end
    

    See also ActiveRecord::Relation::find_or_create_by.