0

我在保存表单数据和包含正确的 account_id 时遇到问题。下面的表格显示了 current_user.account_id 在 dataload_mailchimps 表中有记录的时间。如果访问者将他们的 api 密钥添加到表单中,它应该与 current_user 的 account_id 一起保存到 dataload_mailchimps 表中。

但是,现在 api_key 已正确保存,没有 account_id 保存到 dataload_mailchimps 表中。提交表单时,我应该做些什么不同的事情来将 current_user.account_id 保存到表中?

我的控制器:

class DataloadMailchimpsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @dataload = DataloadMailchimp.new(params[:dataload_mailchimp].merge(
        account_id: current_user.account_id )
    )

    respond_to do |format|
      if @dataload.save
        format.html { redirect_to edit_dataload_mailchimp_path, notice: 'Dataload mailchimp was successfully created.' }
        format.json { render json: @dataload, status: :created, location: @dataload }
      else
        format.html { render action: "new" }
        format.json { render json: @dataload.errors, status: :unprocessable_entity }
      end
    end
  end

表单(_form.html.haml):

= form_for @dataload, :url => dataload_mailchimp_path do |f|
  .fieldset
    .field
      = f.label :api_key
      %br/
      = f.text_field :api_key

  - if @dataload.new_record? || @dataload.api_key.blank?
    %p Please specify Mailchimp API key for additional configuration

  .actions
    = f.submit 'Update Mailchimp Dataload'
4

1 回答 1

1

如果您不想 :account_id 在您attr_accessible的帐户中,则应直接添加帐户:

@dataload.account = current_user.account
于 2013-10-01T06:11:51.593 回答