我有一个公司模型和一个用户模型。A User belongs_to a Company 一个公司有很多用户。创建了一家公司,然后人们可以单独注册,这将创建协会。我试图有一个选择框,因此当用户注册时,他们从选择框中选择他们的公司,该选择框使用公司模型的 pluck 方法填充。
当用户注册时,它说选择框是空的……即使它看起来不是。我究竟做错了什么?
公司模式
class Company < ActiveRecord::Base
attr_accessible :company_admin, :company_name, :employee_number, :street_address, :city, :state_abbreviation, :zip_code
has_many :users
validates :company_admin, presence: true
validates :company_name, presence: true, uniqueness: { case_sensitive: false }
validates :employee_number, presence: true
validates :street_address, presence: true
validates :city, presence: true
validates :state_abbreviation, presence: true
validates :zip_code, presence: true
end
用户模型
class User < ActiveRecord::Base
attr_accessible :company, :name, :email, :password, :password_confirmation, :image, :team_id
belongs_to :company
end
公司控制人
class CompaniesController < ApplicationController
before_filter :signed_in_user, only: [:show, :edit, :update, :destroy]
def show
@company = Company.find(params[:id])
end
def new
@company = Company.new
end
def create
@company = Company.new(params[:company])
if @company.save
redirect_to signup_path
else
render 'new'
end
end
def update
if @company.update_attributes(params[:company])
flash[:success] = "Company Information Updated"
sign_in @company
redirect_to @company
else
render 'edit'
end
end
end
新用户视图
<%= form_for(@user) do |f| %>
<%= f.label :company, :class => 'col-left' %>
<%= collection_select(:company_id, :company_id, Company.all, :id, :company_name, {:include_blank => true}) %>
<%= f.label :email, :class => 'col-left' %>
<%= f.text_field :email, :class => 'col-right' %>
<%= f.label :name, :class => 'col-left' %>
<%= f.text_field :name %>
<%= f.label :password, :class => 'col-left' %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password", :class => 'col-left' %>
<%= f.password_field :password_confirmation %>
<%= image_submit_tag "/assets/create-account.png" %>
<% end %>