简体   繁体   English

跨模型Rails传递对象ID

[英]Passing object ID across models Rails

I am trying to create an app that allows users to create and apply for jobs but seem to have hit a problem. 我正在尝试创建一个允许用户创建和申请工作的应用程序,但似乎遇到了问题。

I can't get the job_id to pass into my apps (job applications) table in my database. 我无法将job_id传递到数据库中的应用程序(作业应用程序)表中。

To get this app to work succesfully I need to pass the job_id and the user_id to the user's application form so that when they submit their job application this information is stored in my apps table. 为了使此应用程序成功运行,我需要将job_id和user_id传递给用户的申请表,以便他们提交工作申请时,此信息存储在我的apps表中。 The job owner will then be able to review the applications they have received. 然后,工作负责人将能够查看他们收到的申请。

I have the following associations in my models: 我的模型中具有以下关联:

 class App < ActiveRecord::Base
 belongs_to :job
 belongs_to :user

 class Job < ActiveRecord::Base
 belongs_to :user
 has_many :apps
 has_many :applicants, :through => :apps, :source => :user

 class User < ActiveRecord::Base
 has_many :apps
 has_many :jobs
 has_many :jobs_applied_for, :through => :apps, :source => :job

Defined on my Jobs controller's show page (the page from which the user can click "apply now" to start an application) I have the following: 在我的Jobs控制器的显示页面(用户可以从中单击“立即申请”以启动应用程序的页面)上定义,我具有以下内容:

def show
  @job = Job.find(params[:id])    
end

The link to "apply now" on the actual page is: 实际页面上“立即申请”的链接为:

<%=link_to "Apply Now", new_app_path %>

and on my Apps controller's new page I have: 在我的Apps控制器的新页面上,我有:

def new
  @user = current_user
  @app = @user.apps.build
end

My user_id is passing perfectly and appearing in my apps table but I am totally stumped on how to pass the job_id correctly. 我的user_id传递完美,并出现在我的应用程序表中,但我完全对如何正确传递job_id感到困惑。

If I have missed anything that I can edit into this question to help you answer it then please do let me know. 如果我错过了任何可以编辑的问题以帮助您回答,请告诉我。

Thanks in advance for your help! 在此先感谢您的帮助!

You are not passing the job_id in your new_app_path link. 您没有在new_app_path链接中传递job_id。 Try changing it to new_app_path(:job_id => @job.id) , and in your controller add @job = Job.find(params[:job_id]) 尝试将其更改为new_app_path(:job_id => @job.id) ,然后在您的控制器中添加@job = Job.find(params[:job_id])

Assuming your routes nest apps inside jobs, your link to the new application page should be something like 假设您的路线将应用程序嵌套在作业中,则指向新应用程序页面的链接应类似于

link_to 'apply now', new_app_job_path(@job)

In your new action you'll have a params[:job_id] that you can use to set @job 在您的新操作中,您将有一个params[:job_id]可用于设置@job

Then on the form, your call to form for should look like 然后在表单上,​​您对表单的调用应如下所示

form_for [@job, @app] do |f|
  ...

This ensures that your create action will also have a :job_id parameter that you can use when creating the application 这样可以确保您的create动作还将具有:job_id参数,您可以在创建应用程序时使用它

The action that you should be invoking is create not new . 你应该调用的作用是createnew

Change the link_to code as follows: 更改link_to代码,如下所示:

<%=link_to "Apply Now", apps_path(:app => {:job_id => @job.id}), :method => :post %>

In your controller: 在您的控制器中:

def create
  app = current_user.apps.build(params[:app])
  if app.save
    # handle success
  else
    # handle error
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM