Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我在rails 4中通过这个简单的创建操作得到一个未定义的方法错误?_Ruby On Rails_Ruby On Rails 4_Undefined_Rails Activerecord_Simple Form - Fatal编程技术网

Ruby on rails 为什么我在rails 4中通过这个简单的创建操作得到一个未定义的方法错误?

Ruby on rails 为什么我在rails 4中通过这个简单的创建操作得到一个未定义的方法错误?,ruby-on-rails,ruby-on-rails-4,undefined,rails-activerecord,simple-form,Ruby On Rails,Ruby On Rails 4,Undefined,Rails Activerecord,Simple Form,我发现了一些关于堆栈溢出的类似问题,但没有任何帮助。我有一个用户模型。我有一个属于用户模型的配置文件模型。我有一个属于个人资料模型的工作模型。我正在制作一个简单的表格来创建一个工作。当我在浏览器中提交表单时,会出现以下错误: undefined method `build_job' for #<Student:0x007f8309023530> jobs create方法与profiles create方法相同,profile一词替换为job,所以我不明白为什么它不起作用。我猜这与

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

undefined method `build_job' for #<Student:0x007f8309023530>
jobs create方法与profiles create方法相同,profile一词替换为job,所以我不明白为什么它不起作用。我猜这与属于另一个模型的工作有关。我该如何解决这个问题?此外,以下是作业参数方法:

def profile_params
      params.require(:profile).permit(:title, :category, :description, :state, :zip_code, :rate, jobs_attributes: [:firm, :position])
end
以下是我的模型:

工作:


有几个问题。您需要在用户上定义
有许多
作业,构建
有许多
作业的正确方法是
关联。构建
而不是
构建关联

class User
  has_many :jobs, through: :profile
end

job = current_user.jobs.build(job_params)

谢谢,这使得表单在提交时不会出错,但当我在视图中引用作业时,仍然会出现相同的错误(几乎是。未定义的方法“job”)。我意识到我忘了在视图中发布引用-我现在就这样做。谢谢您的帮助。我刚刚发布了视图中的行。@Philip7899,这是因为您的个人资料
有许多作业,但您正在引用
profile.job
。您必须循环浏览
profiles.jobs
数组。如果您只想显示最后一份工作:
@user.profile.jobs.last.firm如果@user.profile.jobs.any?
。谢谢,但它不会在视图中显示公司。只是一片空白?我输入了你的代码,这样就可以显示上一份工作的公司。@Philip7899没有代码,我说不出问题出在哪里。您是否在服务器日志中看到错误?
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)
class User
  has_many :jobs, through: :profile
end

job = current_user.jobs.build(job_params)