结束关键字的语法说明

时间:2014-02-25 20:42:37

标签: ruby-on-rails syntax

这是我正在尝试制作的应用程序的文件(来自Hartl教程,它只是一个基本的Twitter应用程序)。直到我在浏览器建议的最底部添加了第三个“结束”时,网站才加载,但我无法弄清楚为什么需要这样做。我得到所有“def”需要一个关键字“end”,但为什么代码末尾有3个“end”。

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

3 个答案:

答案 0 :(得分:1)

end添加到updatecreate方法,因为方法未关闭。 此外,在底部的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

答案 1 :(得分:0)

following并且在create方法中定义了私有方法,该方法是在update方法中创建的。我怀疑你想这样做。因此,文件末尾的三个end关闭create,然后关联update,然后关闭该类。在end条款之后你需要一个if,也许这就是引起混淆的原因。

答案 2 :(得分:0)

  1. 您错过了更新方法的结束(可能是由于缩进造成的疏忽)。
  2. 您错过了创建方法的结尾(可能是由于缩进造成的疏忽)。
  3. 课程本身需要结束
  4. 正确格式化它应该如下:

    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