Ruby on rails 未知属性:使用与用户有多个关联的计划id
***哎呀!我忘记将plan_id迁移到users表 在迈克尔·哈特尔(Michael Hartl)的教程之后,我一直在忙着开发自己的应用程序。现在,有两个表——计划和用户。该计划有很多用户,这些用户属于计划,现在我要做的是完成注册过程,因此当有人通过计划id=?进入注册表单时,它会将其提交到一个隐藏的表单字段,并注册用户 但是,当我尝试访问我的注册路径时,我遇到以下错误:Ruby on rails 未知属性:使用与用户有多个关联的计划id,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,***哎呀!我忘记将plan_id迁移到users表 在迈克尔·哈特尔(Michael Hartl)的教程之后,我一直在忙着开发自己的应用程序。现在,有两个表——计划和用户。该计划有很多用户,这些用户属于计划,现在我要做的是完成注册过程,因此当有人通过计划id=?进入注册表单时,它会将其提交到一个隐藏的表单字段,并注册用户 但是,当我尝试访问我的注册路径时,我遇到以下错误: ActiveRecord::UnknownAttributeError in UsersController#new u
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
class UsersController
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>
注册
计划模型
# == 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
#==架构信息
#
#表名:计划
#
#id:整数不为空,主键
#名称:字符串(255)
#最大电话号码:整数
#最大分钟数:整数
#创建时间:datetime非空
#更新时间:datetime非空
#价格:十进制(,)
#
类计划
用户模型
# 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
#Twilio身份验证凭据
TWILIO_PARENT_ACCOUNT_SID='修订版'#开发版
TWILIO_PARENT_ACCOUNT_TOKEN='##修订版'
#==架构信息
#
#表名:用户
#
#id:整数不为空,主键
#名称:字符串(255)
#电子邮件:string(255)
#创建时间:datetime非空
#更新时间:datetime非空
#密码摘要:字符串(255)
#记住\u标记:字符串(255)
#twilio\u帐户\u sid:字符串(255)
#twilio_auth_令牌:字符串(255)
#
类用户self[:email]})
self.twilio\u account\u sid=@subaccount.sid
self.twilio\u auth\u token=@subaccount.auth\u token
结束
结束
指向您的用户控制器#新
方法的url必须包含一个计划id
部分,后跟一个id
,该id应该由参数[:计划id]
接收。你的网址是这样的吗?您能否将相关部分添加到rake routes
的输出中 我是萨米隆,谢谢你的回答!我意识到我在users表上实际上没有plan_id列,因此导致了问题。