Question

2nd Update

This is the code in my model

after_initialize :start_two_weeks


    def start_two_weeks
        @subscription = TrialSubscription.all 
        @subscription.manual_expiry_date = 
        @subscription.save
    end 

How do you set the time dynamically from Time.now to Time.now - 2 weeks? Its a datetime field what is the value for a datetime field? More Update this means I didn't log out

Filter chain halted as :require_no_authentication rendered or redirected

I logged out and the user did get created but its through the registration controller.....

Updated I just realized that the form is posting to /users. The registrations controller is inheritting from devise::registration

Started POST "/users" for 127.0.0.1 at 2014-03-23 19:58:08 -0400
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"8xhRi7TBEA+zXfmuzuJLqLjjo5JMkyEjUmgmRT6DsOQ=", "user"=>{"email"=>"helloworld@gmail.com", "password"=>"[FILTERED]"}, "commit"=>"Create User"}
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 4.1ms (ActiveRecord: 0.7ms)

The form isn't posting to here

new_trial_subscription GET        /trial_subscriptions/new(.:format)                      trial_subscriptions#new

POST       /trial_subscriptions(.:format)                          trial_subscriptions#create

Ok I am trying to reproduce the stuff that I did in rails console to put them into the controllers.

I have two models

class TrialSubscription < ManualSubscription

    accepts_nested_attributes_for :user
    attr_accessible :user_attributes 

end 

ManualSubscription inherits from Subscription and in Subscription model

belongs_to :user

I am building the user model through subscription because of some other reason.

in my rails console I did these

@subscription = TrialSubscription.new
@user = @subscription.build_user(email: "helloworld@gmail.com", password: "thisiseightcharacters")
@subscription.save

I am able to create a user and its associated trial subscription at the same time. I am trying to reproduce this in the controller and the content will be user inputted.

This is my form in

app/views/trial_subscriptions/new.html.erb

<%= simple_form_for @subscription.user do |f| %>
  <%= f.input :email %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

I added

resources :trial_subscriptions

so the url is trial_subscriptions/new

this is my controller

class TrialSubscriptionsController < ApplicationController

    def index
    end 

    def new
        @subscription = TrialSubscription.new
        @subscription.build_user 
    end 

    def create
        @subscription = TrialSubscription.new(params[:subscription])
        @subscription.email = :email
        @subscription.password = :password
        if @subscription.save 
            flash[:notice] = "Trial Subscription has started"
        else
            flash[:error] = "There was an error creating this subscription, please try again"
        end 
    end 
end 

1) What is happening between new and create?

2) in my create method the params[:subscription] is pointing to the @subscription object in my form is this correct?

3) The :email is actually user attribute, is it getting passed correctly at @subscription.email = :email?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top