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 %>   

助けていただければ幸いです!

4

3 に答える 3

1

私は興味がある; form_for で使用.buildしているため、クライアントは既に URL に含まれている可能性があります。

削除するとどうなりますか:

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

そして提出するとどうなりますか?この行はparamsを間違って見ているので、あなたが作ったものを上書きしているのではないかと思いますform_for

それか、@Adamが言ったような隠しフィールドが機能します。

于 2012-10-15T21:23:35.607 に答える
1

client_id には、ビューのフォームに関連する入力フィールドがありません。次のようなものをフォームに追加できます。

f.hidden_field :client_id

そして、コントローラーで次のように設定します。

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

または、url に client_id を含めることもできます。

于 2012-10-15T21:19:21.177 に答える
0

ばかげた間違いは、レポートのためにフォームを開く前にクライアントがフォームを閉じるために、フォームの終了機能を起動する必要があるようです。

次に、client_id のフィールドを追加し、Adam の提案に従ってフィールドを非表示にします。

これは私がこの間違いを解決するのに役立つので、提案してくれたステフに感謝します。

みんな、ありがとう!:-)

于 2012-10-18T08:56:21.793 に答える