0

我發現堆棧溢出的一些類似的問題,但沒有什麼幫助。我有一個用戶模型。我有一個屬於用戶模型的配置文件模型。我有一個屬於配置文件模型的工作模型。我正在製作一份簡單的表格來創建一份工作。當我在瀏覽器中提交表單,我給出的錯誤:爲什麼我用這個簡單的創建操作在rails 4中得到未定義的方法錯誤?

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 

的就業機會創造方法是相同的配置文件創建方法,用word配置文件替換工作,所以我不明白爲什麼它不工作。我的猜測是它與屬於另一個模型的模型有關。我該如何解決?另外,這裏是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) 

回答

1

幾個問題。您需要在用戶定義has_many工作,爲建設has_many正確的方法是association.buildbuild_association

class User 
    has_many :jobs, through: :profile 
end 

job = current_user.jobs.build(job_params) 
+0

謝謝,這得到了表格提交時不發生錯誤,但我仍然得到同樣的錯誤(當然,差不多。未定義的方法'工作')當我在我看來指的是工作。我意識到我忘了在視圖中發佈參考文獻 - 我現在要做。感謝您的幫助。 – Philip7899

+0

我剛剛從視圖中張貼了一行。 – Philip7899

+0

@ Philip7899這是因爲你的個人資料'has_many'工作,但你正在引用'profile.job'。你將不得不遍歷'profiles.jobs'數組。如果您只想顯示上一份工作:'@ user.profile.jobs.last.firm if @ user.profile.jobs.any?'。 –

相關問題