0

我发现了一些关于堆栈溢出的类似问题,但没有任何帮助。我有一个用户模型。我有一个属于用户模型的配置文件模型。我有一个属于配置文件模型的工作模型。我正在制作一个简单的表格来创建工作。当我在浏览器中提交表单时,出现错误:

undefined method `build_job' for #<Student:0x007f8309023530>

它显示了作业控制器中的创建操作:

def create
    job = current_user.build_job(job_params)
    job.save
    redirect_to profile_path(current_user.profile_name)
end 

工作创建方法与配置文件创建方法相同,将配置文件一词替换为工作,所以我不知道为什么它不起作用。我的猜测是它与属于另一个模型的模型的工作有关。我该如何解决?另外,这里是 job_params 方法:

def profile_params
      params.require(:profile).permit(:title, :category, :description, :state, :zip_code, :rate, jobs_attributes: [:firm, :position])
end

这是我的模型:

工作:

class Job < ActiveRecord::Base
  belongs_to :profile
end

轮廓:

class Profile < ActiveRecord::Base
    belongs_to :user
    has_many :jobs, :dependent => :destroy
end

用户:

class User < ActiveRecord::Base
    has_one :profile
end

参考观点:

<%= @user.profile.job.firm if @user.profile.try(:job)%>

我还通过单击提交来添加我的服务器日志。希望它有助于回答这个问题:

  Started POST "/jobs" for 127.0.0.1 at 2013-10-27 21:45:06 -0400
    ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
  Processing by JobsController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"VRqIuzR1x6tE/G+/wzrG1iFBOEDE7mgsfyjokX7wNZo=", "job"=>{"firm"=>"signat", "position"=>""}, "commit"=>"Save"}
    User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
     (0.2ms)  BEGIN
    SQL (3.4ms)  INSERT INTO "jobs" ("created_at", "firm", "position", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Mon, 28 Oct 2013 01:45:06 UTC +00:00], ["firm", "signat"], ["position", ""], ["updated_at", Mon, 28 Oct 2013 01:45:06 UTC +00:00]]
     (0.4ms)  COMMIT
  Redirected to http://localhost:3000/profiles/philip7899
  Completed 302 Found in 209ms (ActiveRecord: 9.8ms)


  Started GET "/profiles/philip7899" for 127.0.0.1 at 2013-10-27 21:45:06 -0400
  Processing by ProfilesController#show as HTML
    Parameters: {"id"=>"philip7899"}
    User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
    User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."profile_name" = 'philip7899' ORDER BY "users"."id" ASC LIMIT 1
    Profile Load (0.8ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1  [["user_id", 1]]
    School Load (0.9ms)  SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 ORDER BY "schools"."id" ASC LIMIT 1  [["id", 1]]
    Job Exists (0.6ms)  SELECT 1 AS one FROM "jobs" WHERE "jobs"."profile_id" = $1 LIMIT 1  [["profile_id", 1]]
    Rendered profiles/_full_profile.html.erb (92.4ms)
    Rendered profiles/show.html.erb within layouts/application (95.4ms)
    Rendered layouts/_ssi_header_inner.html.erb (4.2ms)
    Rendered layouts/_ssi_footer.html.erb (0.2ms)
  Completed 200 OK in 218ms (Views: 209.4ms | ActiveRecord: 5.9ms)
4

1 回答 1

1

几个问题。您需要has_many在用户上定义作业,而正确的构建has_many方法association.build不是build_association

class User
  has_many :jobs, through: :profile
end

job = current_user.jobs.build(job_params)
于 2013-10-27T21:36:19.310 回答