我正在做Michael Hartl的Rails教程的第7章,我在尝试在开发中注册用户时遇到错误。我已经完成了7.3的章节,所以我的所有测试都应该现在通过,但我仍然在开发中接收The action 'create' could not be found for UsersController
。
这是我的用户控制器
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
end
在本教程的这一点上,我应该可以在线表单中注册用户而不会出现问题。这是我的github存储库https://github.com/ajhausdorf/sample_app
答案 0 :(得分:6)
您的def new
尚未关闭。
此代码应该有效
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end # Here what you missing
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
end
答案 1 :(得分:3)
你的end
没有出现问题:
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end # You were missing one here.
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation)
end
# end # You have an extra one here.
end
答案 2 :(得分:3)
您的new
方法并未在您认为结束的地方结束;它包含create
和user_params
方法。
您已在create
方法中创建了一个名为new
的本地方法,而不是在{{1}上创建名为create
的常规实例方法}。class。
答案 3 :(得分:0)
def new
@user = User.new
end # you are missing this line
def create