如果用户至少有一个然后显示其他内容显示其他
If user has at least one then show something else show other
我正在编写一个基本索引页面,我想向用户显示他们的信用卡列表(如果有的话),否则我想向他们显示添加信用卡按钮。
我知道语句类似于 if current_user >1 credit_card
我有一个使用 devise 的用户模型和一个 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
您已经在 index
操作中使用了此 @credit_cards = current_user.credit_cards
行,这就是为什么您不需要在 index.html.erb
文件中使用 current_user
的原因,因为上面的行 make确定你有或没有信用卡然后你的 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
else
Ruby
The Bastards Book of Ruby
的基本解释
我正在编写一个基本索引页面,我想向用户显示他们的信用卡列表(如果有的话),否则我想向他们显示添加信用卡按钮。
我知道语句类似于 if current_user >1 credit_card
我有一个使用 devise 的用户模型和一个 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
您已经在 index
操作中使用了此 @credit_cards = current_user.credit_cards
行,这就是为什么您不需要在 index.html.erb
文件中使用 current_user
的原因,因为上面的行 make确定你有或没有信用卡然后你的 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
else
Ruby
The Bastards Book of Ruby