gpt4 book ai didi

ruby-on-rails - ||如何工作? : @client = client. 查找(参数[:client_id] || 参数[:id])

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:20 31 4
gpt4 key购买 nike

rails 新手。按照多态关联的教程,我遇到了这个以在createdestroy 中设置@client。

@client = Client.find(params[:client_id] || params[:id])

我通常只习惯你只能找到@client = Client.find(params[:id])

那么这在有两个参数的情况下是如何工作的? ||如何工作?

FavoriteClientsController.rb:

class FavoriteClientsController < ApplicationController

def create
@client = Client.find(params[:client_id] || params[:id])

if Favorite.create(favorited: @client, user: current_user)
redirect_to @client, notice: 'Leverandøren er tilføjet til favoritter'
else
redirect_to @client, alert: 'Noget gik galt...*sad panda*'
end
end

def destroy
@client = Client.find(params[:client_id] || params[:id])

Favorite.where(favorited_id: @client.id, user_id: current_user.id).first.destroy
redirect_to @client, notice: 'Leverandøren er nu fjernet fra favoritter'
end

end

Controller 完整代码,型号可见here

使用 rails 5

最佳答案

表达式:params[:client_id] || params[:id] 等同于:

if params[:client_id]
params[:client_id]
else
params[:id]
end

关于ruby-on-rails - ||如何工作? : @client = client. 查找(参数[:client_id] || 参数[:id]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893106/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com