删除用户未显示的链接

时间:2016-04-01 00:33:32

标签: html ruby-on-rails ruby-on-rails-4

我是网络开发的新手(我现在已经编写了大约6/7个月),希望能够构建自己的应用程序。我一直在阅读和使用Michael Hartl的Ruby on Rails教程。我在第9章删除用户部分并且卡住了。除了用户链接之外,一切都可以工作。它只是没有显示在显示页面上。用户和他们的头像很好。我只是想办法让用户删除链接以显示。下面是用户模型,用户控制器,代码当前用户所在的会话控制器以及视图的代码。

此外,当我从用户部分取出if语句时,删除链接显示。我不确定是什么错,因为我已经定义了admin和current_user。

我会非常感谢任何指导。如果以下信息不够充分,我的项目也在github上

https://github.com/krischery2150/Try2150-master/tree/updating-users

class UsersController < ApplicationController
  before_action :logged_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 show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)
    if @user.save
      log_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
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      flash[:success] = "Profile Updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

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

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

  private

  def user_params
      params.require(:user).permit(:username, :email, :password,
                                  :password_confirmation, :user_about_me,
                                  :birthday, :avatar, :gender)

  end

  ##Before filters method
  # Confirms that a given user is logged in. Only when these conditions are met the user will
  # be able to update or edit their page

  def logged_in_user
    unless logged_in?
      store_location
      flash[:danger]= "Please Log In"
      redirect_to login_url
    end
  end

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

  # Confirms an admin user.
  def admin_user
    redirect_to(root_url) unless current_user.admin?
  end


end

class User < ActiveRecord::Base
    before_save {self.email = email.downcase}
    attr_accessor :remember_token

    has_attached_file :avatar, styles: { medium: "300x300>", thumb: "50x50>" }, default_url: "/images/:thumb/missing.png"
    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
    # this before_save is a callback method. What it does is before it saves the email
    #address it calls back and transforms all the letters into lower case. Had to do the indexing
    #in active record in order for the method to work
    validates :username , presence: true, length: {maximum: 250}
      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
      #code that ensures that a user  puts the right format for emails in signup
      #fields
    validates :email, presence: true, length:{maximum: 50},
                                      format:{with: VALID_EMAIL_REGEX },
                                      uniqueness:{ case_sensitive: false }
                                      #rails still assumes that uniquess is true
                                      #whether the user types CAMELcase or lowercase
    validates :password, presence: true, length:{maximum: 50}, allow_nil: true
    validates :user_about_me, presence: true
    validates :birthday, presence:true
    has_secure_password

    # Returns the hash digest of the given string.
    def User.digest(string)
      cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                    BCrypt::Engine.cost
      BCrypt::Password.create(string, cost: cost)
    end


    ## returns a random user token
    def User.new_token
      SecureRandom.urlsafe_base64
    end

    # Remember a given user to the database for use of persistent sessions

    def remember
      self.remember_token = User.new_token
      update_attribute(:remember_digest, User.digest(remember_token))
    end

    ##returns true if given token matches the digest
    def authenticated?(remember_token)
      return false if remember_digest.nil?
      BCrypt::Password.new(remember_digest).is_password?(remember_token)
    end

    def forget
      update_attribute(:remember_digest, nil)
    end

    def log_out
      forget(current_user)
      session.delete(:user_id)
      @current_user = nil
    end
  end

这是所有用户显示的索引页面上的部分呈现中的代码。

<div class="col-md-9 col-offset-3" id="index-profile">

  <li class="users">
    <div class="col-xs-3 profilepic-container">
      <%= image_tag user.avatar.url %>
    </div>
    <%= link_to user.username, user %>
    <% if current_user.admin? && !current_user?(user) %>
     | <%= link_to "delete", user, method: :delete,
                                   data: { confirm: "You sure?" } %>
      <% end %>
  </li>

1 个答案:

答案 0 :(得分:2)

partial中的删除链接有两个必须为true的条件。用户必须是管理员,并且配置文件不能是他们自己的配置文件。因此,如果管理员用户是唯一的用户,则不会显示删除链接。

尝试创建第二个用户,看看该用户是否显示删除链接。