使用 Ruby on Rails,从另一个 Ruby 类的显示页面上显示表单字段中的数据的最佳方式是什么?
我对 Ruby 还是有点陌生,并且在显示页面上显示来自另一个类的数据时遇到问题。我有两个课程,公司和工作。在“职位显示”页面上,我想在求职者查看相应职位时显示创建/发布职位的公司表单字段中的公司名称、网站和描述。
修改作业显示控制器操作时收到错误。不完全确定公司在创建时是否未分配 ID,或者控制器中的显示操作登录是否存在问题,或者我这边是否存在模型关联错误。非常感谢任何解决此问题的帮助和解释。
模型
class Company < ApplicationRecord
has_many :jobs
has_many :job_applications, through: :jobs
class Job < ApplicationRecord
belongs_to :user
belongs_to :company, optional: true
has_many :job_applications, dependent: :destroy
class JobApplication < ApplicationRecord
belongs_to :user
belongs_to :job
控制器
class CompaniesController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_company
@company = Company.find(params[:id])
# @company = self.create_company
end
# Only allow a list of trusted parameters through.
def company_params
params.require(:company).permit(:name, :website, :about, :user_id, :avatar)
end
class JobsController < ApplicationController
# GET /jobs/1 or /jobs/1.json
def show
@company = Company.find(params[:user_id])
# @company = Company.all
# @job = Job.find(params[:id])
end
路线
resources :companies
resources :jobs
resources :jobs do
resources :job_applications
end
职位展示页面
<%= @company.name %>
<%= @company.website %>
<%= @company.about %>
I'm still somewhat new to Ruby and am having trouble displaying data on the show page from another class. I have two classes, Company and Job. On the Job show page I would like to display the Company's name, website and description from the Company form fields that created/posted the job when a job applicant views the respective job.
Was receiving an error when tinkering with the Job show controller action. Not entirely sure if the company is not being assigned an id when being created or if there's an issue with the show action login in the controller or a model association error on my end. Any help and explanation to resolve this issue is greatly appreciated.
Screenshot for Error Received on Job Show Page
Models
class Company < ApplicationRecord
has_many :jobs
has_many :job_applications, through: :jobs
class Job < ApplicationRecord
belongs_to :user
belongs_to :company, optional: true
has_many :job_applications, dependent: :destroy
class JobApplication < ApplicationRecord
belongs_to :user
belongs_to :job
Controllers
class CompaniesController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_company
@company = Company.find(params[:id])
# @company = self.create_company
end
# Only allow a list of trusted parameters through.
def company_params
params.require(:company).permit(:name, :website, :about, :user_id, :avatar)
end
class JobsController < ApplicationController
# GET /jobs/1 or /jobs/1.json
def show
@company = Company.find(params[:user_id])
# @company = Company.all
# @job = Job.find(params[:id])
end
Routes
resources :companies
resources :jobs
resources :jobs do
resources :job_applications
end
Job Show Page
<%= @company.name %>
<%= @company.website %>
<%= @company.about %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信问题出在
JobsController
中的show
方法。它应该看起来像这样:
这可能会引发一些错误,因为您的关系中有
optional: true
。另外,我不关心 n+1 次查询,因为它只是一条记录,但这可以改进为只有 1 个对数据库的 SQL 查询。I believe the problem lies in your
show
method in theJobsController
.It should look something like this:
This might throw some errors since you have
optional: true
in your relation. Also, I didn't care of n+1 queries since it's just a record, but this could be improved to be only 1 SQL query to the database.