我有以下内容:
客户有许多报告,报告属于一个客户。
但是,在创建报告时,它没有将 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 %>
协助将不胜感激!