gpt4 book ai didi

ruby-on-rails - Rails 关系 (has_many/belongs_to) 已完成

转载 作者:行者123 更新时间:2023-12-04 01:23:33 25 4
gpt4 key购买 nike

所以我已经有很长一段时间没有玩过人际关系了,我想确保我做对了。

在我的客户模型中,我有:

class Client < ApplicationRecord
has_many :projects, dependent: :destroy
end

在我的项目模型中,我有:

class Project < ApplicationRecord
belongs_to :client
end

所以我知道那已经定了。然后抓取我放入项目 Controller 的项目:

def create
@client = Client.find(params[:client_id])
@project = @client.project.new(project_params)
flash[:notice] = "Project created successfully" if @client.project << @project
respond with @project, location: admin_project_path
end

我是否需要在我的节目中加入同样的东西?

关于人际关系我还缺少什么吗?

最佳答案

我会这样想:

def create
@client = Client.find(params[:client_id])
@project = @client.project.new(project_params)
flash[:notice] = "Project created successfully" if @client.project << @project
respond with @project, location: admin_project_path
end

看起来更像是:

def create
@client = Client.find(params[:client_id])
@project = @client.projects.new(project_params)
if @project.save
# do success stuff
else
# do failure stuff
end
end

注意

@project = @client.project.new(project_params)

应该是:

@project = @client.projects.new(project_params)

正如 Yechiel K 所说,不需要做:

@client.project << @project

开始于:

@project = @client.projects.new(project_params)

将在新的@project 上自动设置client_id。顺便说一句,如果你想手动添加一个projectclient,那么它是:

@client.projects << @project

(注意 projectsproject。)

如果没有带有params[:client_id]client,那么@client = Client.find(params[:client_id] ) 会抛出错误。您可能应该包括一个救援 block 。或者,我更喜欢:

def create
if @client = Client.find_by(id: params[:client_id])
@project = @client.projects.new(project_params)
if @project.save
# do success stuff
else
# do failure stuff
end
else
# do something when client not found
end
end

此外,respond with 也不是问题。 respond_with 是一回事。 (我相信它已被移动到一个单独的 gem,responders。)如果您需要不同的响应,例如,对于 htmljs,您的代码并不清楚。如果不是,那么我认为它更像是:

def create
if @client = Client.find_by(id: params[:client_id])
@project = @client.projects.new(project_params)
if @project.save
flash[:notice] = "Project created successfully"
redirect_to [@client, @project]
else
# do failure stuff
end
else
# do something when client not found
end
end

这假设您的路线看起来像:

Rails.application.routes.draw do

resources :clients do
resources :projects
end

end

在这种情况下,rails 会将 [@client, @project] 解析为正确的路由/路径。

正如 DaveMongoose 提到的,您可以@client = Client.find_by(id: params[:client_id]) 移动到 before_action 中。这很常见。 Here's关于为什么不这样做的讨论。就个人而言,我曾经像这样使用 before_action,但现在不用了。作为替代方案,您可以这样做:

class ProjectsController < ApplicationController 

...

def create
if client
@project = client.projects.new(project_params)
if @project.save
flash[:notice] = "Project created successfully"
redirect_to [client, @project]
else
# do failure stuff
end
else
# do something when client not found
end
end

private

def client
@client ||= Client.find_by(id: params[:client_id])
end

end

更进一步,您可以:

class ProjectsController < ApplicationController 

...

def create
if client
if new_project.save
flash[:notice] = "Project created successfully"
redirect_to [client, new_project]
else
# do failure stuff
end
else
# do something when client not found
end
end

private

def client
@client ||= Client.find_by(id: params[:client_id])
end

def new_project
@new_project ||= client.projects.new(project_params)
end

end

关于ruby-on-rails - Rails 关系 (has_many/belongs_to) 已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241278/

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