无法为UsersController找到“创建”操作

时间:2013-09-24 17:48:54

标签: ruby-on-rails railstutorial.org

我正在做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

4 个答案:

答案 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方法并未在您认为结束的地方结束;它包含createuser_params方法。

您已在create方法中创建了一个名为new 的本地方法,而不是在{{1}上创建名为create的常规实例方法}。class。

答案 3 :(得分:0)

def new
  @user = User.new
end  # you are missing this line

def create