2013-09-30 49 views
0

我在保存表單數據幷包含正確的account_id時遇到問題。下面的表格顯示current_user.account_id何時在dataload_mailchimps表中有記錄。如果訪問者將其api鍵添加到表單中,則應將其與current_user的account_id一起保存到dataload_mailchimps表中。如何使用Devise在Rails中創建方法的範圍?

但是,現在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' 
+0

你使用的是attribute_accessible的東西嗎?在那裏允許'account_id'?你是否嘗試直接設置它? '@dataload.account = current_user.account'? – phoet

+0

@phoet你的建議是正確的。謝謝你的幫助。我將account_id添加到attr_accessible中,並將account_id賦值分隔開來並且工作。你想添加你的建議作爲答案,我會接受嗎? – analyticsPierce

回答

1

如果你不希望有:ACCOUNT_ID在attr_accessible,那麼你應該直接添加帳戶:

@dataload.account = current_user.account 
相關問題