如果用户至少有一个,那么显示其他东西显示其他

时间:2018-03-09 00:58:54

标签: ruby-on-rails ruby-on-rails-5.1

我正在写一个基本的索引页面,我想向用户显示他们的信用卡列表,如果他们有,否则我想向他们显示一个添加信用卡按钮。

我知道这句话就像if current_user> 1 credit_card

我有使用设计和信用卡模型的用户模型,基本上我想显示以下内容。

<% if current_user has atleast 1(>1) credit_card>

show this

<% else %>

show that

<% end %>

Credit_Cards控制器

class CreditCardsController < ApplicationController

  before_action :authenticate_user!
  before_action :set_credit_card, only: [:show, :edit, :update, :destroy]

  # GET /credit_cards
  # GET /credit_cards.json
  def index
    @credit_cards = current_user.credit_cards
    @credit_card_debt = current_user.credit_cards.sum(:card_balance)
    @credit_limit = current_user.credit_cards.sum(:card_limit)
    @available_credit = current_user.credit_cards.sum(:card_limit) - current_user.credit_cards.sum(:card_balance)
  end



  # GET /credit_cards/1
  # GET /credit_cards/1.json
  def show
  end

  # GET /credit_cards/new
  def new
    @credit_card = current_user.credit_cards.build
  end

  # GET /credit_cards/1/edit
  def edit
  end

  # POST /credit_cards
  # POST /credit_cards.json
  def create
    @credit_card = current_user.credit_cards.new(credit_card_params)

    respond_to do |format|
      if @credit_card.save
        format.html { redirect_to @credit_card, notice: 'Credit card was successfully created.' }
        format.json { render :show, status: :created, location: @credit_card }
      else
        format.html { render :new }
        format.json { render json: @credit_card.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /credit_cards/1
  # PATCH/PUT /credit_cards/1.json
  def update
    respond_to do |format|
      if @credit_card.update(credit_card_params)
        format.html { redirect_to @credit_card, notice: 'Credit card was successfully updated.' }
        format.json { render :show, status: :ok, location: @credit_card }
      else
        format.html { render :edit }
        format.json { render json: @credit_card.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /credit_cards/1
  # DELETE /credit_cards/1.json
  def destroy
    @credit_card.destroy
    respond_to do |format|
      format.html { redirect_to credit_cards_url, notice: 'Credit card was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_credit_card
      @credit_card = CreditCard.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def credit_card_params
      params.require(:credit_card).permit(:card_name, :card_provider, :points_provider, :interest_rate, :card_balance, :card_limit, :user_id )
    end
end

信用卡模式

class CreditCard < ApplicationRecord

  belongs_to :user

  def credit_available
    card_limit - card_balance
  end

  def annual_interest
    card_balance * interest_rate
  end


  def minimum_payment
    n = card_balance / 100
    b = n * 1
    i = card_balance * interest_rate
    c = i / 12

    c + b
  end

  def total_monthly_payment
    total_monthly_payment = 0
    @total_monthly_payment = total_monthly_payment + current_user.credit_cards.minimum_payment.sum

  end
end

1 个答案:

答案 0 :(得分:1)

您已在@credit_cards = current_user.credit_cards操作中使用了此index行,这就是您不需要在current_user文件上使用index.html.erb的原因因为上面的行确保您有信用卡或没有,那么您的HTML文件看起来像

<% if @credit_cards.count >= 1 %>
    show this
<% else %>
    show that
<% end %>

或者你可以使用这样的东西

<% if @credit_cards.any? %>
    show this
<% else %>
    show that
<% end %>

或者你可以使用这样的东西

<% if current_user.credit_cards.count >= 1 %> #=> or current_user.credit_cards.any?
    show this
<% else %>
    show that
<% end %>

@credit_cards = current_user.credit_cards行动起,您不需要index

以下是if The Bastards Book of Ruby

的一些else Ruby基本说明