2012-10-15 187 views
0

我有以下內容:屬於/有很多關係

客戶端有許多報告和報告屬於客戶端。

但是,在創建報告時,它並未將client_id分配到數據庫中,但不知道爲什麼?

我在這裏做錯了什麼?

客戶端模型

class Client < ActiveRecord::Base 
    has_many :reports, :dependent => :destroy 
end 

報表模型

class Report < ActiveRecord::Base 
    has_attached_file :report 
    belongs_to :client 

end 

客戶機控制器(更新)

# PUT /clients/1 
    # PUT /clients/1.json 
    def update 
    @client = Client.find(params[:id]) 

    respond_to do |format| 
     if @client.update_attributes(params[:client]) 
     format.html { redirect_to [:admin,@client], :notice => 'Client was successfully updated.' } 
     format.json { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.json { render :json => @client.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

報告控制器(創建)

# POST /reports 
    # POST /reports.json 
    def create 
    @report = Report.new(params[:report]) 
    @report.client_id = params[:client][:client_id] 

    respond_to do |format| 
     if @report.save 
     format.html { redirect_to '/admin/clients', :notice => 'Report was successfully created.' } 
     format.json { render :json => @report, :status => :created, :location => @report } 
     else 
     format.html { render :action => "new" } 
     format.json { render :json => @report.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

客戶端,編輯,查看

<%= form_for([:admin, @client.reports.build]) do |f| %> 
    <label class="formlabel">Report Upload</label> 
    <%= f.file_field :report, :class=>"text-input small-input" %> 
    <div class="actions"> 
    <br /> 
    <%= f.submit 'Upload', :class => 'button' %> 
    </div> 
<% end %> 

援助將不勝感激!

+0

你可以驗證是否爲'params [:client] [:client_id]'設置了一個值嗎?如果不是這樣,那麼你可以通過將'@ client.reports.build'設置爲nil來取消它的工作。 AFAIK當使用'.build'方法初始化子對象時,它應該在運行'.save'時爲你自動設置foreign_key。 – Noz

+0

您可以向我們展示您的創建/更新操作的參數嗎? – Noz

+0

不知道我是否理解你的意思,更新操作是否在 – Clay

回答

1

我很好奇;因爲您在form_for中使用了.build,所以客戶端可能已經在url中。

,如果你刪除什麼:

@report.client_id = params[:client][:client_id] 

並提交,那麼什麼會發生?由於此行是在PARAMS尋找錯誤,所以我不知道,如果要覆蓋您在form_for

要麼,建立像@Adam隱藏字段表示會工作。

+0

嗨Steph 如果我刪除它在數據庫中創建報告記錄,但client_id字段爲空(因此不創建鏈接到客戶端) 謝謝 – Clay

+0

當你運行'rake routes'時,這是什麼路線路徑?它是'/ clients /:client_id/reports/new'?請告訴我你耙地時路線是什麼,以及它說的是什麼控制器等。 –

+0

嗨路線是/ clients /:clients_id /編輯,我試圖在編輯操作中做到這一點客戶控制器。然而,我已經從數據庫和form_for中刪除了列,現在正在尋找任何可行的方法。你會建議什麼 – Clay

1

client_id在您的視圖中沒有相關的輸入字段。你可以講一下你的格式如:

f.hidden_field :client_id 

然後在你的控制器,將其設置爲:

@report.client_id = params[:report][:client_id] 

或者,你可以在URL中包含的CLIENT_ID。

+0

嗨Adam 客戶端ID位於URL中,因爲它在客戶端模型的編輯功能中。/clients /:id/edit – Clay

+0

好的,所以它就像/ clients /:client_id/reports/new?如果是這樣,那麼你可以做@ report.client_id = params [:client_id]。這不一定是世界上最安全的事情(除非你有一些控制,他們可以通過任何他們想要的客戶端ID),但它應該工作。 – Adam

+0

對不起,URL只是客戶端/ 1 /編輯,並且在此視圖中是報表的上載表單,不需要額外的URL。現在嘗試,但不成功,我沒有得到任何錯誤,但報告表中的client_id仍爲空 – Clay

0

愚蠢的錯誤它似乎需要在表單上的最終功能 - 客戶端打開表單之前將其關閉 - 用於報告。

然後爲client_id添加字段,現在按照Adam的建議隱藏該字段。

感謝Steph的建議,因爲這有助於我解決這個錯誤。

謝謝大家! :-)

+0

很高興你知道了粘土! –