discipleartem February 2016

Ruby on Rails Tutorial chapter 9.4 (add admin user)

Please help with error

Error:

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 ')'

My /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 =         

Answers


K M Rakibul Islam February 2016

Change your app/views/users/_user.html.erb code to the following:

<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>

Post Status

Asked in February 2016
Viewed 3,662 times
Voted 5
Answered 1 times

Search




Leave an answer