有人可以解释以下 Ruby on Rails 错误吗?
Could someone explain the following Ruby on Rails error?
CandidatesController#create 中的加载错误
Unable to autoload constant Usermailer
, expected Z:/railsassignment/student/app/mailers/usermailer.rb
to define it
当我提交表单时出现上述错误。该表单处理一条记录,候选人被添加到数据库中,但是我试图发送给新注册候选人的欢迎电子邮件没有发送,上面的错误阻止了用户继续。
候选人控制器
def create
@candidate = Candidate.new(candidate_params)
respond_to do |format|
if @candidate.save
Usermailer.welcome(@candidate).deliver_now ***<-- Error highlights this line***
format.html { redirect_to @candidate, notice: 'User was successfully
created.' }
format.json { render :show, status: :created, location: @candidate }
else
format.html { render :new }
format.json { render json: @candidate.errors, status:
:unprocessable_entity }
end
end
end
usermailer.rb
Z:/railsassignment/student/app/mailers/usermailer.rb(用户邮件目录)
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def welcome(candidate)
@candidate = candidate
mail(:to => candidate.can_email, :subject => "Welcome to EmployeMe.com, You
have registered successfully!")
end
end
如果您需要查看更多文件,请给我留言,我会尽快将它们添加到问题中。
这似乎是一个乱七八糟的命名约定。
根据 Rails
命名约定,文件名 应在 snake_case
和 class 名称 在 CamelCase
中。在您的场景中,文件名应为 user_mailer.rb
.
所以要么将 usermailer.rb
重命名为 user_mailer.rb
要么
class 名字 UserMailer
到 Usermailer
。
文件名始终使用蛇形大小写。
Z:/railsassignment/student/app/mailers/user_mailer.rb
不是:
Z:/railsassignment/student/app/mailers/usermailer.rb
class 姓名始终使用驼峰式大小写
UserMailer.welcome(@candidate).deliver_now
不是:
Usermailer.welcome(@candidate).deliver_now
如果您对常量加载如何与 rails 一起工作感兴趣,您可以查看 this。
CandidatesController#create 中的加载错误
Unable to autoload constant
Usermailer
, expectedZ:/railsassignment/student/app/mailers/usermailer.rb
to define it
当我提交表单时出现上述错误。该表单处理一条记录,候选人被添加到数据库中,但是我试图发送给新注册候选人的欢迎电子邮件没有发送,上面的错误阻止了用户继续。
候选人控制器
def create
@candidate = Candidate.new(candidate_params)
respond_to do |format|
if @candidate.save
Usermailer.welcome(@candidate).deliver_now ***<-- Error highlights this line***
format.html { redirect_to @candidate, notice: 'User was successfully
created.' }
format.json { render :show, status: :created, location: @candidate }
else
format.html { render :new }
format.json { render json: @candidate.errors, status:
:unprocessable_entity }
end
end
end
usermailer.rb
Z:/railsassignment/student/app/mailers/usermailer.rb(用户邮件目录)
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def welcome(candidate)
@candidate = candidate
mail(:to => candidate.can_email, :subject => "Welcome to EmployeMe.com, You
have registered successfully!")
end
end
如果您需要查看更多文件,请给我留言,我会尽快将它们添加到问题中。
这似乎是一个乱七八糟的命名约定。
根据 Rails
命名约定,文件名 应在 snake_case
和 class 名称 在 CamelCase
中。在您的场景中,文件名应为 user_mailer.rb
.
所以要么将 usermailer.rb
重命名为 user_mailer.rb
要么
class 名字 UserMailer
到 Usermailer
。
文件名始终使用蛇形大小写。
Z:/railsassignment/student/app/mailers/user_mailer.rb
不是:
Z:/railsassignment/student/app/mailers/usermailer.rb
class 姓名始终使用驼峰式大小写
UserMailer.welcome(@candidate).deliver_now
不是:
Usermailer.welcome(@candidate).deliver_now
如果您对常量加载如何与 rails 一起工作感兴趣,您可以查看 this。