Your new action looks correct, your create action needs some update. Let's first start from your association then move on to the form and then to your create
action.
In the form view code, you are building a form for @subscription.user
. The simple_form_for
method would translate this to create
action in UsersController
. But you do not have this and your intention is to create user
through TrialSubscriptionsController
.
So in order to make this happen, you need to change the form so that when it renders and/or submits it creates the form for trial_subscription
along with the defined user_attributes
within trial_subscription
. So, update your view to:
<%= simple_form_for @subscription do |f| %>
<%= f.simple_fields_for :user do |u| %>
<%= u.input :email %>
<%= u.input :password %>
<% end %>
<%= f.button :submit %>
<% end %>
The changes are simple_form_for @subscription.user
to simple_form_for @subscription
, and wrapping email
and password
fields with f.simple_fields_for :user
. With these changes, when your form submits, it will execute the create
action of the TrialSubscriptionsController
.
The next change is on the TrialSubscriptionsController#create
action.
def create
@subscription = TrialSubscription.new(params[:trial_subscription])
if @subscription.save
flash[:notice] = "Trial Subscription has started"
else
flash[:error] = "There was an error creating this subscription, please try again"
end
end
The changes here are @subscription = TrialSubscription.new(params[:subscription])
to @subscripition = TrialSubscription.new(params[:trial_subscription])
, this is because your object is of type TrialSubscription
not Subscription
.
The second change is removal of the following lines:
@subscription.email = :email
@subscription.password = :password
This is because, as you say, email
and password
are members of user
not trial_subscription
.
Hopefully this helps.