2014-11-06 17 views
0

我不確定我在這裏做錯了什麼。基本上,用戶只能由現有用戶創建,他們只能在現有用戶所在的公司中。因此,我希望f.select for:company來自current_user.companies。模型通過表「users_companies」加入。Rails 4表格從關係模型中選擇

形式:

<div class="row"> 
    <h3>Add User</h3> 
    <div class="column"> 
     <%= simple_form_for @user do |f| %> 
     <%= f.input :name %> 
     <%= f.input :email %> 
     <%= f.select(:company_id, @companies.map {|company| [company,company]}) %> 
     <%= f.select(:role, User.roles.keys.map {|role| [role,role]}) %> 
     <%= f.input :password %> 
     <%= f.input :password_confirmation %> 
     <%= f.button :submit %> 
     <% end %> 
    </div> 

控制器:

def new 
    @user = User.new 
    @companies = current_user.companies 
    end 

    def create 
    @user = User.new(params[:user].permit(:name, :company, :email, :role, :password, :password_confirmation)) 
    authorize @user 
    @companies = current_user.companies 

    if params[:user][:password].blank? 
     params[:user].delete(:password) 
     params[:user].delete(:password_confirmation) 
    end 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to users_path, notice: 'User was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
    end 

模式:

ActiveRecord::Schema.define(version: 20140620190817) do 

    create_table "companies", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "groups", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "company_id" 
    t.integer "tag_id" 
    t.integer "q_rcode_id" 
    t.integer "page_id" 
    end 

    create_table "pages", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "company_id" 
    t.integer "group_id" 
    t.integer "tag_id" 
    t.integer "q_rcode_id" 
    end 

    create_table "q_rcodes", force: true do |t| 
    t.string "url" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "company_id" 
    t.integer "group_id" 
    t.integer "page_id" 
    end 

    create_table "tags", force: true do |t| 
    t.string "url" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "company_id" 
    t.integer "group_id" 
    t.integer "page_id" 
    end 

    create_table "users", force: true do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.string "name" 
    t.integer "role" 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 

    create_table "users_companies", id: false, force: true do |t| 
    t.integer "user_id" 
    t.integer "company_id" 
    end 

    add_index "users_companies", ["user_id", "company_id"], name: "index_users_companies_on_user_id_and_company_id" 
    add_index "users_companies", ["user_id"], name: "index_users_companies_on_user_id" 

end 

回答

0

我想通了,而不必改變模型。我結束了使用collection_select方法,例如:

<div class="row"> 
    <h3>Add User</h3> 
    <div class="column"> 
    <%= simple_form_for @user do |f| %> 
     <%= f.input :name %> 
     <%= f.input :email %> 
     <%= collection_select(:user, :company_ids, 
     current_user.companies, :id, :name) %> 
     <%= f.select(:role, User.roles.keys.map {|role| [role,role]}) %> 
     <%= f.input :password %> 
     <%= f.input :password_confirmation %> 
     <%= f.button :submit %> 
    <% end %> 
    </div> 
</div> 

而且從控制器拉出的定義,留下了這一點:

def create 
    @user = User.new(params[:user].permit(:name, :company_ids, :email, :role, :password, :password_confirmation)) 
    authorize @user 

    if params[:user][:password].blank? 
     params[:user].delete(:password) 
     params[:user].delete(:password_confirmation) 
    end 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to users_path, notice: 'User was successfully created.' } 
     else 
     format.html { render action: "new" } 
     end 
    end 
    end 
1

好吧,有一個問題。如果用戶和公司通過user_company建立多對多關係,則無法告知用戶何時是公司的「所有者」,以及何時該用戶是公司的「員工」。你需要兩個不同的關聯。

添加USER_ID到公司

添加belongs_to的關聯公司模型

class Company < ActiveRecord::Base 
    has_many :users, :through => :company_user #these are the employees of the company 
    belongs_to: :user #this is the user who owns the company 
end 

解決您的company_user數據,以便它僅包含其中,用戶是公司

的員工解決貴公司的數據記錄所以user_id字段包含作爲公司所有者的用戶的ID

您的用戶模型應該已經擁有一個公司的關聯

class User < ActiveRecord::Base 
      has_many :companies, :through => :company_user #these are the companies the user is employeed by 
     end 
  • 現在「@companies」在你的新動作將企業用戶通過employeed名單。
+0

我得到這樣的:沒有這樣的列:companies.user_id:SELECT 「公司」。* FROM「companies」WHERE「companies」。「user_id」= 1 – socialconquest 2014-11-06 19:09:58

+0

我添加了模式 - 我認爲這是問題來自何處,它們通過users_companies相關。 – socialconquest 2014-11-06 19:15:44

+0

我很抱歉,給了你錯誤的信息。由於該關聯是'通過'user_company,所以公司中不會有user_id。我會解決我的答案。 – septerr 2014-11-06 19:30:56