繁体   English   中英

Rails 4形式从关系模型中选择

[英]Rails 4 form select from relational model

我不确定我在做什么错。 基本上,用户只能由现有用户创建,并且只能位于现有用户所在的公司中。因此,我希望:company的f.select来自current_user.companies。 通过表“ users_companies”连接模型。

形成:

  <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

好的,有问题。 如果用户和公司之间通过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”将具有该用户所雇用的公司的列表。

我知道了这一点,而不必更改模型。 我最终使用了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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM