2012-10-02 55 views
0

***已解決糟糕!我忘了將plan_id移植到用戶表...未知屬性:與用戶使用has_many關聯時的plan_id

在Michael Hartl的教程後,我一直在竊聽我自己的應用程序。現在,有兩個表格 - 計劃和用戶。該計劃has_many用戶,用戶屬於計劃,現在我想要做的是完成註冊過程,所以當有人通過plan_id =?進入註冊表單時,它會將其提交給隱藏的表單字段,並且註冊用戶。

然而,當我試圖訪問我的signup_path,我得到這個錯誤:

ActiveRecord::UnknownAttributeError in UsersController#new 

unknown attribute: plan_id 

我不知道如何用它來做什麼。會愛你的想法!

用戶控制器

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 

查看/用戶/ 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> 

計劃模型

# == 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 

用戶模型

# 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+\-.][email protected][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 

回答

0

您的UsersController#new方法的網址必須包含一個plan_id部分,後面跟着id,應該由params[:plan_id]接收。你的網址是這樣嗎?你可以添加rake routes輸出的相關部分嗎?

+0

I Samiron,謝謝你的回答!我意識到我沒有在用戶表上實際上有一個plan_id列,因此導致了問題。 –