Collection_select form_for表示它是空白的,当它不是时

时间:2012-10-22 01:57:28

标签: ruby-on-rails-3.2 form-for html-select

我有公司模型和用户模型。用户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 %>

1 个答案:

答案 0 :(得分:0)

我不得不加f。到集合选择所以它是

f.collection_select(:company_id, Company.all, :id, :company_name, {:include_blank => true})