NoMethodError(nil:NilClass 的未定义方法“posts”)
NoMethodError (undefined method `posts' for nil:NilClass)
我是 rails 的新手,正在使用 gem griddler 来解析一些收到的电子邮件。我使用了 github 站点中的所有代码,它在此处提供了一个示例 emailprocessor.rb:
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
我知道我需要以某种方式和某处声明 posts,但我在这里和 rails 网站上四处寻找,但没有找到任何可以帮助我创建它的东西。当 运行 heroku logs -t 当电子邮件服务器尝试 post 到页面时,我在标题中收到错误。
以下是我可能需要的其余文件以及我尝试过的文件:
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
非常感谢任何帮助,我想知道我做错了什么,这样我以后就可以避免了
在数据库中找不到电子邮件地址,因此 user
为零。您可以使用以下代码即时创建用户:
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
我是 rails 的新手,正在使用 gem griddler 来解析一些收到的电子邮件。我使用了 github 站点中的所有代码,它在此处提供了一个示例 emailprocessor.rb:
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
我知道我需要以某种方式和某处声明 posts,但我在这里和 rails 网站上四处寻找,但没有找到任何可以帮助我创建它的东西。当 运行 heroku logs -t 当电子邮件服务器尝试 post 到页面时,我在标题中收到错误。
以下是我可能需要的其余文件以及我尝试过的文件:
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
非常感谢任何帮助,我想知道我做错了什么,这样我以后就可以避免了
在数据库中找不到电子邮件地址,因此 user
为零。您可以使用以下代码即时创建用户:
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