2014-02-19 96 views
0

強參數我有以下設計的登記表(與不相關的標記省略):困惑與設計

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { id: "payment-form" }) do |f| %> 
    <%= f.fields_for resource.paid_account do |pa| %> 
    <%= pa.collection_select :account_plan_id, @account_plans, :id, :name_with_price %> 
    <% end %> 
<% end %> 

這種形式吐出以下HTML:

<select id="user_paid_account_account_plan_id" name="user[paid_account][account_plan_id]"> 
    <option value="2">Lite ($10.00/mo)</option> 
    <option value="3">Professional ($20.00/mo)</option> 
    <option value="4">Plus ($30.00/mo)</option> 
</select> 

我然後有以下代碼我ApplicationController

class ApplicationController < ActionController::Base 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(
     :email, 
     :password, 
     :paid_account => :account_plan_id # <-- THIS IS PROBABLY WRONG 
    ) 
    end 
    end 
end 

當我提交我的形式,我得到「PaidAccount( #2163736620)預計,得到ActionController :: Parameters(#2173083140)「。

我也試着不給錯誤的下面,但它也沒有創造一項紀錄:

u.permit(
    :email, 
    :password, 
    :paid_account => [] 
) 

我在做什麼錯?

+0

我還沒有工作你想的辦法,我只好把消毒的設計這個RegistrationController內,如: '高清sign_up_params params.require(:用戶).permit(:電子郵件,:密碼,:password_confirmation,:current_password,....) end' – sissy

回答

0

顯示你的兩個模型(UserPaidAccount,我相信)會有很大的幫助。無論如何,假設UserPaidAccount一比一的關係,代碼可能應該是這樣的:

class User < ActiveRecord::Base 
    has_one :paid_account 

    accepts_nested_attributes_for :paid_account 
end 

class PaidAcount < ActiveRecord::Base 
    belongs_to :user 
end 

class ApplicationController < ActionController::Base 
    before_filter :configure_permitted_parameters, if: :devise_controller? 

    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) do |u| 
     u.permit(
     :email, 
     :password, 
     :paid_account_attributes => :account_plan_id 
    ) 
    end 
    end 
end 

看看你的日誌來查看錶單值是如何被提交。 StrongPameters gemActive Record's Nested Attributes的文檔也應該有所幫助。

+0

您的回答是正確的。事實證明,問題的一部分是,我的表單命名'paid_account'字段,而沒有'_attributes'部分,它拋出所有內容。 –