This is the error I get:
NoMethodError in Devise::RegistrationsController#create
undefined method `trial_duration' for nil:NilClass
app/models/user.rb:120:in `set_trial_end'
This is the relevant parts of my User.rb
before_create :set_trial_end
def set_trial_end
plan = self.plan
end_of_trial = Date.today + self.plan.trial_duration.days
self.trial_end_date = end_of_trial.to_date
end
What’s strange is that if I look in my DB, for a user that is assigned a plan and look at the trial_duration attribute, I get a reply:
ruby-1.9.2-p0 > tu
=> #<User id: 29, email: "test50@abc.com", encrypted_password: "$2a$10$TKKQmBYem.vDq.mwDutv2u92Vick0X0jAIUQT6A9.FM....", password_salt: "$2a$10$TKKQmBYem.vDq.mwDutv2u", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2011-06-18 23:03:12", last_sign_in_at: "2011-06-18 23:03:12", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", username: "test50", first_name: "Test", last_name: "Fifty", created_at: "2011-06-18 23:02:48", updated_at: "2011-06-18 23:03:12", invitation_token: nil, invitation_sent_at: nil, plan_id: 2, current_state: nil, confirmation_token: nil, confirmed_at: "2011-06-18 23:03:12", confirmation_sent_at: "2011-06-18 23:02:47", space_used: 0, failed_attempts: 0, unlock_token: nil, locked_at: nil, trial_end_date: "2011-07-02", active_subscription: nil, customer_id: nil>
ruby-1.9.2-p0 > tu.plan
=> #<Plan id: 2, name: "Boutique", storage: 100.0, num_of_projects: 99999, num_of_clients: 10, cached_slug: "boutique", created_at: "2011-01-31 09:46:57", updated_at: "2011-08-11 08:22:48", amount: 19, trial_duration: 14, trial_duration_unit: "days", currency: "USD", billing_cycle: 28, billing_cycle_unit: "days">
ruby-1.9.2-p0 > tu.plan.trial_duration
=> 14
ruby-1.9.2-p0 > tu.plan.trial_duration.days
=> 14 days
So not sure why Rails is giving me that error.
Any ideas?
Edit 1:
In my view, I have this:
<% if params[:promo] %>
<%= text_field_tag "xcode", nil, :placeholder => "Coupon Code" %><br />
<%= hidden_field_tag(:plan_id, "1") %>
<% end %>
And in the POST, from my log, you can see the parameter plan_id set to 1
Started POST "/users" for 127.0.0.1 at 2011-08-24 03:11:07 -0500
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Jp2GHxnuVOVnI/sfr1CB4EQ9URCTJynv/2Ek4AiU8Lg=", "user"=>{"username"=>"test.user", "first_name"=>"Testing", "last_name"=>"User", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"testuser@abc.com"}, "xcode"=>"testcouponcode", "plan_id"=>"1", "commit"=>"Register"}
nil
But I am still getting the error Plan can't be blank on the next page.
I have added the after_validation callback instead of after_create, so I no longer the NoMethodError. But the problem isn’t solved.
You’re using a
before_createhook that’s assuming thatself.planis set:And your error says:
So
self.planisnilduring yourset_trial_endcall.Perhaps you want an
after_createhook so that you’ll have some instance data to work with. Even then you’d want to check forself.plan.nil?(just in case) and use a validation to make sure you get one:Or maybe an
after_validationhook would serve you better.