Ruby on Rails教程第9.4章(添加管理员用户)

时间:2016-02-08 15:39:55

标签: ruby-on-rails ruby ruby-on-rails-4 railstutorial.org

请帮助解决错误

错误:

SyntaxError in UsersController#index

app/views/users/_user.html.erb:4: syntax error, unexpected ')', expecting keyword_then or ';' or '\n' ...dmin? && !current_user?(user) );@output_buffer.safe_append=' ... ^ 

app/views/users/_user.html.erb:7: syntax error, unexpected keyword_end '.freeze;@output_buffer.append=( end );@output_buffer.safe_append=' ^ 

/app/views/users/_user.html.erb:10: syntax error, unexpected keyword_ensure, expecting ')' 

/app/views/users/_user.html.erb:12: syntax error, unexpected keyword_end, expecting ')'

我的/app/controllers/users_controller.rb

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

  def new
    @user = User.new
  end

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

  def show
    @user = User.find(params[:id])
  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 edit
    # @user = User.find(params[:id])
  end

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

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

  private

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

    #Before filters

    def signed_in_user
      redirect_to signin_url, notice: "Please sign in." unless signed_in?
    end

    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

应用程序/视图/用户/ _user.html.erb

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
  <%= if current_user.admin? && !current_user?(user) %>
    | <%= link_to 'delete', user, method: :delete,
                                  data: { confirm: "You sure?" } %>
  <%= end %>
</li>

1 个答案:

答案 0 :(得分:0)

将您的app/views/users/_user.html.erb代码更改为以下内容:

<li>
  <%= gravatar_for user, size: 52 %>
  <%= link_to user.name, user %>
  <% if current_user.admin? && !current_user?(user) %>
      <%= link_to 'delete', user, method: :delete,
                    data: { confirm: "You sure?" } %>
  <% end %>
</li>