在Rails中保存外键更新

时间:2014-01-28 10:05:49

标签: ruby-on-rails model-view-controller ruby-on-rails-4 foreign-keys

我有两个表:客户和付款。

我从客户那里拨打新的付款方式:

<%= link_to "Add Payment", controller: 'payments', action: 'new', client_id: client.id %>

工作正常但是当我尝试更新该付款时,不会保存client_id字段。 在支付控制器中,我有:

  def update
    @payment.client_id = @payment.client.id

    respond_to do |format|
      if @payment.update(payment_params)
        format.html { redirect_to @payment, notice: 'Payment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @payment.errors, status: :unprocessable_entity }
      end
    end
  end

我认为这会有效,因为在付款展示操作中,我用

调用客户端对象
@client = @payment.client

为什么类似的通话在更新中无效?

修改

为避免混淆,第一行是下表的一部分:

<tbody>
    <% @clients.each do |client| %>
      <tr>
        <td><%= client.company_name %></td>
        <td><%= client.owner %></td>
        <td><%= client.main_contact_name %></td>
        <td><%= client.phone %></td>
        <td>
          <ul id="options_list">
            <li><%= link_to "Add Payment", controller: 'payments', action: 'new', client_id: client.id %></li>
            <li><%= link_to "View/Manage Payments", controller: 'clients', action: 'show_payments', id: client %></li>
            <li><%= link_to "XML Document", controller: 'clients', action: 'show_sepa', id: client %></li>
          </ul>
        </td>
        <td>
          <ul id="options_list">
            <li><%= link_to 'Show', client %></li>
            <li><%= link_to 'Edit', edit_client_path(client) %></li>
            <li><%= link_to 'Destroy', client, method: :delete, data: { confirm: 'Are you sure?' } %></li>
          </ul>
        </td>
      </tr>
    <% end %>
  </tbody>

编辑2:

请求的代码(部分付款)

<%= form_for @payment do |f| %>
  <% if @payment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@payment.errors.count, "error") %> prohibited this payment from being saved:</h2>

      <ul>
      <% @payment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>

  <fieldset id="form">
    <span><legend>Fill In The Form Below</legend></span>

    <%= f.hidden_field('client_id', :value => params[:client_id]) %>

    <div id="field">
      <%= f.label :signup_fee %>
      <%= f.text_field :signup_fee %>
    </div>

    <div id="field">
      <%= f.label :monthly_fee %>
      <%= f.text_field :monthly_fee %>
    </div>

    <div id="field">
      <%= f.label :date_of_payment, 'Payment Start Date' %>
      <%= f.date_select(:date_of_payment, :order => [:year, :month, :day]) %>
    </div>

    <div id="field">
      <%= f.label :payment_reference %>
      <%= f.text_field :payment_reference %>
    </div>

    <div id="field">
      <%= f.label :remittance_information, 'Remittance Information (optional)' %>
      <%= f.text_field :remittance_information %>
    </div>

    <div id="field">
      <%= f.label :mandate_id, "Mandate ID" %>
      <%= f.text_field :mandate_id %>
    </div>

    <div id="field">
      <%= f.label :mandate_date_of_signature %>
      <%= f.date_select(:mandate_date_of_signature, :order => [:year, :month, :day]) %>
    </div>

    <div id="field">
      <%= f.label 'Type of Payment:' %>
      <%= f.label :is_recurring, 'Recurring' %> 
      <%= f.check_box :is_recurring %>
      <%= f.label :is_onceoff, 'Once Off' %>
      <%= f.check_box :is_onceoff %>
    </div>    

    <div id="field">
      <%= f.label :batch_booking %>
      <%= f.check_box :batch_booking %>
    </div>

  </fieldset>
<% end %>

1 个答案:

答案 0 :(得分:0)

(已编辑以获取更多信息)

<%= link_to "Add Payment", new_client_payment_path(client) %>

这只有在你的路线设置为嵌套的情况下才有效:

resources :clients do
  resources :payments
end

你应该能够删除这一行 - 它没有帮助你

@payment.client_id = @payment.client.id