2016-08-24 78 views
0

嘗試在我的CRM項目中創建新作業時出現無方法錯誤。當我在一個月前開始在公司工作時,我參與了這個項目,幾次檢查過代碼庫,沒有看到問題或者事情沒有加在一起。我覺得自己因爲沮喪而忽視了它,所以我正在尋求一些經驗豐富的Rails開發人員的幫助。提前致謝!JobsController中的NoMethodError#new

作業控制器的有關部分發生錯誤:

# GET /jobs/new 
    def new 
    @job = Job.opportunity.new do |j| 
     if params[:opportunity_id].present? 
     j.opportunity_id = params[:opportunity_id] 
     end 

任務/新視圖:

<% @job[:opportunity_id] = params[:opportunity_id] %> 
<% title "New #{@job.opportunity.name} Job"%> 

<% 
@job[:name] = @job.opportunity.name 
@pm = @job.opportunity.pm_id 


%> 

<br><br> 
<%= render 'form' %> 

工作模式:

class Job < ActiveRecord::Base 
    mount_uploader :file1, AttachmentUploader 
    belongs_to :cost_proposal 
    belongs_to :opportunity 

    def Job 
    has_many :opportunities 
    end 
end 

職位表中schema.rb:

create_table 'jobs', force: true do |t| 
    t.integer 'cost_proposal_id' 
    t.string 'number' 
    t.string 'name' 
    t.date  'flight_date' 
    t.string 'flight_sub' 
    t.string 'camera' 
    t.string 'roll' 
    t.string 'map_type' 
    t.integer 'plan_only' 
    t.integer 'lab_only' 
    t.integer 'est_hrs_model' 
    t.date  'due_date' 
    t.integer 'edge_job_id' 
    t.integer 'custom_trans' 
    t.integer 'comp_inhouse' 
    t.date  'delivered_date' 
    t.integer 'done' 
    t.date  'control_in' 
    t.string 'control_status' 
    t.date  'at_date' 
    t.string 'control_results' 
    t.integer 'control_check' 
    t.string 'scan_staff' 
    t.date  'scan_date' 
    t.integer 'scan_check' 
    t.string 'comp_staff' 
    t.date  'comp_date' 
    t.integer 'comp_check' 
    t.string 'comp_sub' 
    t.date  'comp_sub_due_date' 
    t.integer 'comp_sub_rec' 
    t.string 'img_staff' 
    t.date  'img_date' 
    t.integer 'img_check' 
    t.string 'edit_staff' 
    t.date  'edit_date' 
    t.integer 'edit_check' 
    t.text  'notes' 
    t.string 'file1' 
    t.string 'file2' 
    t.string 'file3' 
    t.string 'file4' 
    t.string 'file5' 
    t.string 'add_files' 
    t.datetime 'created_at' 
    t.datetime 'updated_at' 
    t.integer 'flown' 
    t.integer 'cust_trans' 
    t.integer 'delivered' 
    t.string 'at_staff' 
    t.integer 'at_check' 
    t.integer 'opportunity_id' 
    end 
+1

什麼是錯誤,在什麼情況下發生? – PoloniculMov

+0

@PoloniculMov job = Job.opportunity.new do | j |是發生錯誤的地方。 – kmaune

回答

1

您的關聯需要一定的關注。在你的工作模式中,你既有機會,也有機會。這不是如何工作。您可以在機會集合中擁有primary_opportunity,但具有屬性集。

首先,這是錯誤的:

def Job 
    has_many :opportunities 
    end 

不應該有一個名爲招聘(大寫J)方法,以大寫字母開頭的單詞是類和常量,而不是方法。 類方法(def Job)沒有實例範圍。這不是一個協會的地方。在你的def Job裏面放置方法來創建在實例化之前可訪問的方法。關聯應該駐留在實例方法定義中(def Job之外)移除def作業並結束。這會給你的模型:

class Job < ActiveRecord::Base 
    mount_uploader :file1, AttachmentUploader 
    belongs_to :cost_proposal 
    belongs_to :opportunity 
    has_many :opportunities 
end 

從那裏,你需要確定,如果你想有一個belongs_to :opportunityhas_many :opportunities關係。如果每個工作都有很多工作,請刪除belongs_to

其次,您的@job = Job.opportunity.new do |j|行正在調用一個常量而不是實例的方法。將此行更改爲:

@job = Job.new 
@job.opportunties.new do |j| 

上面的代碼是假設你使用has_many變種的複製關聯。

希望有所幫助。

+0

謝謝,我會給這個鏡頭。這是我沒有寫的所有代碼,所以我必須清理它並使其正常工作。再次感謝,我很感激。 – kmaune

+0

我已經認識到項目中的關聯需要一些主要的關注。再次感謝。 – kmaune