I'm working on adding an "account plan" select field to a Devise sign-up form.
My user model is called User
and my account plan model is called AccountPlan
. Since the majority of users are expected to not actually be accountholders, User
is connected to AccountPlan
via PaidAccount
, i.e. User
has one AccountPlan
through PaidAccount
.
class User < ActiveRecord::Base
has_one :paid_account
has_one :account_plan, through: :paid_account
end
And then here's the relevant part of my form:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { id: "payment-form" }) do |f| %>
<%= f.collection_select :account_plan, @account_plans, :id, :name_with_price %>
<% end %>
My problem is that when I submit the form (which was working perfectly up until I added the :account_plan
field), I get
undefined method `id' for "2":String
It tells me that the offending line is super
, which is of course not particularly helpful. I figured out that the offending line is this, so it evidently doesn't like something about the params.
The confusing thing, though, is that if I do User.new(account_plan: AccountPlan.new)
on the console, it takes that just fine. So if my form is spitting out this HTML
<select id="user_account_plan" name="user[account_plan]"><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>
then I don't get what the problem should be.
If it helps, here's my controller code:
class RegistrationsController < Devise::RegistrationsController
def new
@account_plans = AccountPlan.order(:price)
super
end
def create
super
Stripe.api_key = Rails.configuration.stripe_secret_key
Stripe::Customer.create(
card: params[:stripeToken],
description: resource.email
)
end
end