2012-10-15 123 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

很高兴你知道了粘土! –