Why do I get the error: TypeError in UsersController#create can't convert String into Hash
Why do I get the error: TypeError in UsersController#create can't convert String into Hash
我正在尝试实现电子邮件功能,以便当有人注册时他们会收到一封电子邮件。我正在遵循本指南:http://guides.rubyonrails.org/action_mailer_basics.html
但我收到错误消息:
TypeError in UsersController#create
can't convert String into Hash
application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default "from@example.com"
layout 'mailer'
end
user_mailer.rb:
class UserMailer < ApplicationMailer
default from: 'notifications@example.com'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
views/user_mailer/welcome_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
views/user_mailer/welcome_email.txt.erb
Welcome to example.com, <%= @user.name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
以及users_controller中的创建方法:
def create
@user = User.new(user_params)
if @user.save
# login is achieved by saving a user's 'id' in a session variable,
# accessible to all pages
UserMailer.welcome_email(@user).deliver_later
session[:user_id] = @user.id
redirect_to films_path
else
render action: "new"
end
end
User_params 在 user_controller:
def user_params
params.require(:user).permit(:password, :password_confirmation, :role, :first_name, :last_name, :house_no, :street, :town, :postcode, :email, :date_of_birth)
end
完整错误:
TypeError in UsersController#create
can't convert String into Hash
Extracted source (around line #0):
Rails.root: C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/ThorCinema
Application Trace | Framework Trace | Full Trace
app/mailers/application_mailer.rb:2:in `<class:ApplicationMailer>'
app/mailers/application_mailer.rb:1:in `<top (required)>'
app/mailers/user_mailer.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:33:in `create'
有人可以帮忙吗。
default
引用 ActionMailer
中的散列
所以这一行是不正确的,因为它指定的是字符串而不是散列
default "from@example.com"
应该是
default sender: "from@example.com"
参见
中的"Default Hash"部分
http://api.rubyonrails.org/classes/ActionMailer/Base.html
编辑
请注意,您的完整轨迹甚至为您指出了直线...
app/mailers/application_mailer.rb:2:在`'
我正在尝试实现电子邮件功能,以便当有人注册时他们会收到一封电子邮件。我正在遵循本指南:http://guides.rubyonrails.org/action_mailer_basics.html
但我收到错误消息:
TypeError in UsersController#create
can't convert String into Hash
application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default "from@example.com"
layout 'mailer'
end
user_mailer.rb:
class UserMailer < ApplicationMailer
default from: 'notifications@example.com'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
views/user_mailer/welcome_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.name %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.login %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
views/user_mailer/welcome_email.txt.erb
Welcome to example.com, <%= @user.name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
以及users_controller中的创建方法:
def create
@user = User.new(user_params)
if @user.save
# login is achieved by saving a user's 'id' in a session variable,
# accessible to all pages
UserMailer.welcome_email(@user).deliver_later
session[:user_id] = @user.id
redirect_to films_path
else
render action: "new"
end
end
User_params 在 user_controller:
def user_params
params.require(:user).permit(:password, :password_confirmation, :role, :first_name, :last_name, :house_no, :street, :town, :postcode, :email, :date_of_birth)
end
完整错误:
TypeError in UsersController#create
can't convert String into Hash
Extracted source (around line #0):
Rails.root: C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/ThorCinema
Application Trace | Framework Trace | Full Trace
app/mailers/application_mailer.rb:2:in `<class:ApplicationMailer>'
app/mailers/application_mailer.rb:1:in `<top (required)>'
app/mailers/user_mailer.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:33:in `create'
有人可以帮忙吗。
default
引用 ActionMailer
所以这一行是不正确的,因为它指定的是字符串而不是散列
default "from@example.com"
应该是
default sender: "from@example.com"
参见
中的"Default Hash"部分http://api.rubyonrails.org/classes/ActionMailer/Base.html
编辑
请注意,您的完整轨迹甚至为您指出了直线...
app/mailers/application_mailer.rb:2:在`'