***已解决 哎呀!我忘记将 plan_id 迁移到 users 表...
在 Michael Hartl 的教程之后,我一直在窃听自己的应用程序。现在,有两个表——计划和用户。计划 has_many users 和 users belongs_to Plans,现在我要做的是完成注册过程,所以当有人通过 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+\-.]+@[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