0
votes

***SOLVED Oops! I forgot to migrate plan_id over to the users table...

After Michael Hartl's tutorial, I've been bugging away at an app of my own. Right Now, there are two tables -- Plans, and Users. The plan has_many users, and the users belong_to Plans, and now what I'm trying to do is finish the registration process, so when someone goes to the signup form via a plan_id=?, it submits it to a hidden form field, and registers the user.

However, when I try to visit my signup_path, I get this error :

ActiveRecord::UnknownAttributeError in UsersController#new

unknown attribute: plan_id

I'm not quite sure what to make of it. Would love your thoughts!

Users Controller

class UsersController < ApplicationController

  before_filter :signed_in_user, only: [:show]
  before_filter :correct_user,   only: [:show]

    def show
    @user = User.find(params[:id])
    end

    def new
  plan = Plan.find(params[:plan_id])
    @user = plan.users.build
    end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def index

    if current_user
      redirect_to(user_path(current_user))
    else
    redirect_to(root_path)
    end
  end

  private

    def signed_in_user
      unless signed_in?
        store_location
        redirect_to login_url, notice: "Please sign in."
      end
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end
end

Views/users/new.html.erb

<% provide(:title, 'Sign up') %>
<div class="contentstripe">
<div class="container">
  <div class="row">
   <h1>Sign Up </h1>
  </div>
</div>
</div>

<div class="container">
 <div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>

      <%= f.hidden_field :plan_id %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>
</div>

Plan Model

# == Schema Information
#
# Table name: plans
#
#  id                :integer          not null, primary key
#  name              :string(255)
#  max_phone_numbers :integer
#  max_minutes       :integer
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  price             :decimal(, )
#

class Plan < ActiveRecord::Base
  has_many :users
end

User Model

# Twilio authentication credentials
TWILIO_PARENT_ACCOUNT_SID = '##redacted' ##Development
TWILIO_PARENT_ACCOUNT_TOKEN = '##redacted'

# == Schema Information
#
# Table name: users
#
#  id                 :integer          not null, primary key
#  name               :string(255)
#  email              :string(255)
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  password_digest    :string(255)
#  remember_token     :string(255)
#  twilio_account_sid :string(255)
#  twilio_auth_token  :string(255)
#

class User < ActiveRecord::Base
  belongs_to :plan
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token
  before_save :create_twilio_subaccount

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: true

  validates :password, presence: true, length: { minimum: 6 }
  validates :password_confirmation, presence: true
  validates_presence_of :plan_id

  private

      def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
      end

    def create_twilio_subaccount     
      @client = Twilio::REST::Client.new(TWILIO_PARENT_ACCOUNT_SID, TWILIO_PARENT_ACCOUNT_TOKEN)
      @subaccount = @client.accounts.create({:FriendlyName => self[:email]})
      self.twilio_account_sid = @subaccount.sid
      self.twilio_auth_token  = @subaccount.auth_token
    end

end
1

1 Answers

0
votes

The url to your UsersController#new method must contain a plan_id part followed by a id which is supposed to be received by params[:plan_id]. Is your url like that? Can you add your related part of the output of rake routes.