当我点击"创建用户"时出现以下错误按钮。
Error:
ActiveModel::ForbiddenAttributesError
Extracted source (around line #27):
25 # POST /users.json
26 def create
27 @user = User.new(params[:user])
28 respond_to do |format|
29 if @user.save
我遵循以下参考链接并输入相同的命令和相同的代码。 http://railscasts.com/episodes/206-action-mailer-in-rails-3?view=asciicast
我的代码片段如下所述。
在app / controller / user_controller.rb
中 class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
UserMailer.registration_confirmation(@user).deliver
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email)
end
end
在config / initializer / setup_mail.rb
中ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "w5call.w5rtc@gmail.com",
:password => "w5rtc123@",
:authentication => "plain",
:enable_starttls_auto => true
}
在app / mailer / user_mailer.rb
中 class UserMailer < ActionMailer::Base
default :from => "w5call.w5rtc@gmail.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registered", :from => "w5call.w5rtc@gmail.com")
end
end
应用\视图\ user_mailer文件\ registration_confirmation.text.erb
<%= @user.name %>,
Thank you for registering!
请帮我解决此错误,因为我对ROR完全不熟悉。在此先感谢。
答案 0 :(得分:1)
在Rails 4中有强参数概念。
要解决此问题,您需要更改create
方法并改为调用@user = User.new(user_params)
方法。
答案 1 :(得分:0)
更改
def create
@user = User.new(params[:user])
到
def create
@user = User.new(user_params)