使用 Ruby on Rails,从另一个 Ruby 类的显示页面上显示表单字段中的数据的最佳方式是什么?

发布于 2025-01-17 02:45:52 字数 1670 浏览 5 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信愁 2025-01-24 02:45:54

我相信问题出在 JobsController 中的 show 方法。

它应该看起来像这样:

class JobsController < ApplicationController

   # GET /jobs/1 or /jobs/1.json
  def show
     @job = Job.find(params[:id])
     @company = @job.company
  end

这可能会引发一些错误,因为您的关系中有 optional: true 。另外,我不关心 n+1 次查询,因为它只是一条记录,但这可以改进为只有 1 个对数据库的 SQL 查询。

I believe the problem lies in your show method in the JobsController.

It should look something like this:

class JobsController < ApplicationController

   # GET /jobs/1 or /jobs/1.json
  def show
     @job = Job.find(params[:id])
     @company = @job.company
  end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文