2013-09-24 86 views
0

我正在做Michael Hartl的Rails教程的第7章,並且在嘗試註冊開發中的用戶時出現錯誤。我已經完成了7.3章節,所以我的所有測試都應該現在通過,但我仍然在收到The action 'create' could not be found for UsersController的開發。無法爲UsersController找到操作'create'

這裏是我的用戶控制器

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

回答

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 
3

end s爲走出低谷:

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 
3

new方法並沒有結束,你認爲它結束;它包含了createuser_params方法。

您已經創建了一個名爲createnew方法的本地方法,而不是建立在UserController類稱爲create一個普通實例方法。

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

def create 
相關問題