***已解决哎呀!我忘了将plan_id迁移到users表...
在Michael Hartl的教程之后,我一直在寻找自己的应用程序。现在,有两个表 - 计划和用户。该计划有很多用户,用户属于计划,现在我要做的就是完成注册过程,所以当有人通过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
答案 0 :(得分:0)
UsersController#new
方法的网址必须包含plan_id
部分,后跟id
,params[:plan_id]
应该收到rake routes
。你的网址是这样的吗?您可以添加{{1}}输出的相关部分吗?