Ruby on rails 如果用户至少有一个,则显示其他内容显示其他内容
我正在写一个基本的索引页面,我想向用户显示他们的信用卡列表(如果他们有),否则我想向他们显示一个添加信用卡按钮 我知道这种说法有点像如果当前用户>1张信用卡 我有使用Desive的用户模型和一个信用卡模型,基本上我想展示以下内容Ruby on rails 如果用户至少有一个,则显示其他内容显示其他内容,ruby-on-rails,ruby-on-rails-5.1,Ruby On Rails,Ruby On Rails 5.1,我正在写一个基本的索引页面,我想向用户显示他们的信用卡列表(如果他们有),否则我想向他们显示一个添加信用卡按钮 我知道这种说法有点像如果当前用户>1张信用卡 我有使用Desive的用户模型和一个信用卡模型,基本上我想展示以下内容 <% if current_user has atleast 1(>1) credit_card> show this <% else %> show that <% end %> 1)信用卡> 展示这个 显示 信用
<% if current_user has atleast 1(>1) credit_card>
show this
<% else %>
show that
<% end %>
1)信用卡>
展示这个
显示
信用卡管理员
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 CreditCardsController
信用卡模式
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
class信用卡
您已经使用了此@credit\u cards=current\u user.credit\u cards
索引操作中的行,这就是为什么您不需要在index.html.erb
文件中使用current\u user
,因为上面的行确保您有信用卡或没有,那么您的html文件看起来像
<% if @credit_cards.count >= 1 %>
show this
<% else %>
show that
<% end %>
=1%>
展示这个
显示
或者你可以用这样的东西
<% 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 %>
展示这个
显示
或者你可以用这样的东西
<% 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 %>
=1%>#=>或当前用户。信用卡。有吗?
展示这个
显示
现在你不需要这个@credit\u cards=当前用户。credit\u cards
fromindex
操作
这里是你可以看到的一些
if
else
Rubyif@credit\u cards的基本解释。任何?
都应该做到这一点。