Ruby on rails 表单提交后,如何访问控制器上嵌套属性的表单_?
我需要访问嵌套属性的字段\u,但我一直遇到以下错误:Ruby on rails 表单提交后,如何访问控制器上嵌套属性的表单_?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要访问嵌套属性的字段\u,但我一直遇到以下错误: nil:NilClass的未定义方法“[]” 对于行: params[:braintree_subscription_attributes][0][:plan_token] params["braintree_subscription_attributes"][0]["plan_token"] braintree_account_params[:braintree_subscription_attributes][0][:plan_token
nil:NilClass的未定义方法“[]”
对于行:
params[:braintree_subscription_attributes][0][:plan_token]
params["braintree_subscription_attributes"][0]["plan_token"]
braintree_account_params[:braintree_subscription_attributes][0][:plan_token]
braintree_account_params["braintree_subscription_attributes"][0]["plan_token"]
params[:braintree_subscription_attributes]['0'][:plan_token]
params["braintree_subscription_attributes"]['0']["plan_token"]
braintree_account_params[:braintree_subscription_attributes]['0'][:plan_token]
braintree_account_params["braintree_subscription_attributes"]['0']["plan_token"]
Parameters: {"utf8"=>"✓", "braintree_account"=>{"first_name"=>"asfd", "last_name"=>"asfd", "braintree_subscription_attributes"=>{"plan_token"=>"4zfr"}}, "user_id"=>"1"}
class BraintreeAccount < ApplicationRecord
has_one :braintree_subscription
accepts_nested_attributes_for :braintree_subscription
end
class BraintreeSubscription < ApplicationRecord
belongs_to :braintree_account
end
braintree\u帐户\u参数[:braintree\u订阅\u属性][0][:计划\u令牌]
我试过:
params[:braintree_subscription_attributes][0][:plan_token]
params["braintree_subscription_attributes"][0]["plan_token"]
braintree_account_params[:braintree_subscription_attributes][0][:plan_token]
braintree_account_params["braintree_subscription_attributes"][0]["plan_token"]
params[:braintree_subscription_attributes]['0'][:plan_token]
params["braintree_subscription_attributes"]['0']["plan_token"]
braintree_account_params[:braintree_subscription_attributes]['0'][:plan_token]
braintree_account_params["braintree_subscription_attributes"]['0']["plan_token"]
Parameters: {"utf8"=>"✓", "braintree_account"=>{"first_name"=>"asfd", "last_name"=>"asfd", "braintree_subscription_attributes"=>{"plan_token"=>"4zfr"}}, "user_id"=>"1"}
class BraintreeAccount < ApplicationRecord
has_one :braintree_subscription
accepts_nested_attributes_for :braintree_subscription
end
class BraintreeSubscription < ApplicationRecord
belongs_to :braintree_account
end
没有一个奏效
这:
params[:braintree\u帐户][:braintree\u订阅\u属性]。第一个[:计划\u令牌]
错误:ActionController的未定义方法优先#
我的参数:
params[:braintree_subscription_attributes][0][:plan_token]
params["braintree_subscription_attributes"][0]["plan_token"]
braintree_account_params[:braintree_subscription_attributes][0][:plan_token]
braintree_account_params["braintree_subscription_attributes"][0]["plan_token"]
params[:braintree_subscription_attributes]['0'][:plan_token]
params["braintree_subscription_attributes"]['0']["plan_token"]
braintree_account_params[:braintree_subscription_attributes]['0'][:plan_token]
braintree_account_params["braintree_subscription_attributes"]['0']["plan_token"]
Parameters: {"utf8"=>"✓", "braintree_account"=>{"first_name"=>"asfd", "last_name"=>"asfd", "braintree_subscription_attributes"=>{"plan_token"=>"4zfr"}}, "user_id"=>"1"}
class BraintreeAccount < ApplicationRecord
has_one :braintree_subscription
accepts_nested_attributes_for :braintree_subscription
end
class BraintreeSubscription < ApplicationRecord
belongs_to :braintree_account
end
强参数:
参数require(:braintree\u account).permit(…,braintree\u subscription\u属性:[:id,:plan\u token])
型号:
params[:braintree_subscription_attributes][0][:plan_token]
params["braintree_subscription_attributes"][0]["plan_token"]
braintree_account_params[:braintree_subscription_attributes][0][:plan_token]
braintree_account_params["braintree_subscription_attributes"][0]["plan_token"]
params[:braintree_subscription_attributes]['0'][:plan_token]
params["braintree_subscription_attributes"]['0']["plan_token"]
braintree_account_params[:braintree_subscription_attributes]['0'][:plan_token]
braintree_account_params["braintree_subscription_attributes"]['0']["plan_token"]
Parameters: {"utf8"=>"✓", "braintree_account"=>{"first_name"=>"asfd", "last_name"=>"asfd", "braintree_subscription_attributes"=>{"plan_token"=>"4zfr"}}, "user_id"=>"1"}
class BraintreeAccount < ApplicationRecord
has_one :braintree_subscription
accepts_nested_attributes_for :braintree_subscription
end
class BraintreeSubscription < ApplicationRecord
belongs_to :braintree_account
end
class BraintreeAccount
如何访问计划令牌?因为这是一个have-one,而不是have-many,所以属性中的嵌套对象不会超过1个。仅当预期有多个子项时,才需要使用数组语法
braintree_account_params[:braintree_subscription_attributes][:plan_token]
请提供完整的控制器代码,因为解决方案就在那里。您发布的参数
就是您应该使用/调查的内容,以找到如何访问发布的braintree\u订阅
。然后,更进一步:确保你的强参数没有阻塞所需的字段(但这似乎没问题)。也很容易根据发布的参数进行验证,imho这是正确的。