2014-02-25 43 views
0

這是我正在嘗試製作的應用程序的文件(來自Hartl教程,它只是一個基本的推特應用程序)。該網站沒有加載,直到我在瀏覽器建議的底部添加了第三個'結束',但我無法弄清楚爲什麼這是必需的。我得到所有的「def」需要有一個關鍵字「結束」,但爲什麼在代碼的末尾有3個「結束」。最終關鍵字的句法解釋

class UsersController < ApplicationController 
    before_action :signed_in_user, 
       only: [:index, :edit, :update, :destroy, :following, :followers] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: :destroy 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 


    def show 
     @user = User.find(params[:id]) 
     @microposts = @user.microposts.paginate(page: params[:page]) 
    end 

    def destroy 
     User.find(params[:id]).destroy 
     flash[:success] = "User deleted." 
     redirect_to users_url 
    end 

    def new 
     @user = User.new 
    end 

    def edit 
    end 

     def update 
     if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
     else 
     render 'edit' 
     end 


     def create 
     @user = User.new(user_params) 
     if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
     else 
     render 'new' 
     end 

    def following 
     @title = "Following" 
     @user = User.find(params[:id]) 
     @users = @user.followed_users.paginate(page: params[:page]) 
     render 'show_follow' 
    end 

     def followers 
     @title = "Followers" 
     @user = User.find(params[:id]) 
     @users = @user.followers.paginate(page: params[:page]) 
     render 'show_follow' 
     end 



    private 

     def user_params 
     params.require(:user).permit(:name, :email, :password, 
            :password_confirmation) 
     end 

     # Before filters 


     def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_url) unless current_user?(@user) 
     end 

     def admin_user 
     redirect_to(root_url) unless current_user.admin? 
     end 
end 
end 
end 

回答

1

添加endupdatecreate方法的方法沒有關閉。 此外,在底部的3 end,刪除兩個end只剩下one結束爲您的類。

def update 
     if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
     else 
     render 'edit' 
     end 
    end ## This end was missing 

    def create 
     @user = User.new(user_params) 
     if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
     else 
     render 'new' 
     end 
    end ## This end was missing 
+0

我只是回答了一個非常類似的問題。常見的模式是縮進代碼很少。 +1 –

0

following和私營方法進行的create方法,這是在update方法內部產生內部限定。我懷疑你想這樣做。所以三個end s在文件末尾關閉create,然後update,然後是類。 if條款後需要end,這可能是造成混淆的原因。

0
  1. 你錯過了在更新方法(可能造成不良壓痕監督)結束。
  2. 您錯過了創建方法的結尾(可能是由於縮進不良造成的疏忽)。
  3. 類本身需要一個結束

格式正確的應該是這樣的:

class UsersController < ApplicationController 
    before_action :signed_in_user, 
       only: [:index, :edit, :update, :destroy, :following, :followers] 
    before_action :correct_user, only: [:edit, :update] 
    before_action :admin_user,  only: :destroy 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 


    def show 
    @user = User.find(params[:id]) 
    @microposts = @user.microposts.paginate(page: params[:page]) 
    end 

    def destroy 
    User.find(params[:id]).destroy 
    flash[:success] = "User deleted." 
    redirect_to users_url 
    end 

    def new 
    @user = User.new 
    end 

    def edit 
    end 

    def update 
    if @user.update_attributes(user_params) 
     flash[:success] = "Profile updated" 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def following 
    @title = "Following" 
    @user = User.find(params[:id]) 
    @users = @user.followed_users.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    def followers 
    @title = "Followers" 
    @user = User.find(params[:id]) 
    @users = @user.followers.paginate(page: params[:page]) 
    render 'show_follow' 
    end 

    private 

    def user_params 
    params.require(:user).permit(:name, :email, :password, 
           :password_confirmation) 
    end 

    # Before filters 

    def correct_user 
    @user = User.find(params[:id]) 
    redirect_to(root_url) unless current_user?(@user) 
    end 

    def admin_user 
    redirect_to(root_url) unless current_user.admin? 
    end 
end