如何传递参数以更新动作?

时间:2015-09-24 11:19:11

标签: ruby-on-rails ruby-on-rails-4

在我的应用程序中,用户有很多公司,公司有很多用户,公司有很多部门,用户在不同的公司有不同的部门,例如id为1的用户属于公司1和2区以及2公司2区3 User.rb

class User < ActiveRecord::Base
  has_many :companies, through: :users_companies
  has_many :users_companies
  has_many :divisions, through: :users_divisions
  has_many :users_divisions
  has_many :users_roles, dependent: :destroy
  has_many :roles, through: :users_roles
end

Company.rb

class Company < ActiveRecord::Base

  #before_create :create_division

  has_many :users_companies
  has_many :users, through: :users_companies
  has_many :divisions, dependent: :destroy
  has_many :positions
end

Division.rb

class Division < ActiveRecord::Base
  has_ancestry
  belongs_to :company
  has_many :users, through: :users_divisions
  has_many :users_divisions
end

UsersCompany.rb

class UsersCompany < ActiveRecord::Base
  belongs_to :user
  belongs_to :company
end

UsersDivision.rb

class UsersDivision < ActiveRecord::Base
  belongs_to :user
  belongs_to :division
  belongs_to :company
end

UsersDivision表有结构 ID | USER_ID | division_id | COMPANY_ID

当创造一切都很好

UsersDivision.create!(user: @user, division_id: params[:user][:division_ids], company_id: @company.id)

但是当我尝试在表Usersdivision列中更新@user时,company_id将是nill。

def update
    @company = Company.find(params[:company_id])
    @user = User.find(params[:id])

    if @user.update(user_params)
      redirect_to :back
    end
  end

如何传递params:company_id来更新动作?

<div class="edit-registration-container">
<%= @user.full_name %>
<%= form_for([@company, @user]) do |f| %>
<%= f.label :last_name, "Фамилия" %><br />
    <%= f.text_field :last_name, type: "text" %>

    <%= f.label :first_name, "Имя" %><br />
    <%= f.text_field :first_name, type: "text" %>

    <%= f.label :middle_name, "Отчество" %><br />
    <%= f.text_field :middle_name, type: "text" %>
    <%= f.label :division_ids, "Подразделение" %><br />
    <%= f.select :division_ids, @divisions_select %><br />
    <%= f.hidden_field :company_id, value: @company.id %>
    <%= f.fields_for :positions, @position do |ff| %>
    <%= ff.label :name, "Должность" %>
    <%= ff.text_field :name, type: "text" %>
    <%= ff.hidden_field :company_id, value: @company.id %>
    <% end %>
    <%= hidden_field_tag "user[role_ids][]", nil %>
    <% Role.all.each do |role| %>
      <%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id), id: dom_id(role) %>
      <%= label_tag dom_id(role), role.name %><br>
      <% end %>
    <%= f.submit "Сохранить", class: "login loginmodal-submit", type: "submit"  %>
<% end %>
</div>

1 个答案:

答案 0 :(得分:1)

  1. 您需要允许该参数可访问,因此请将其添加到您的 用户控制器参数允许:

    params.require(:user).permit(YOUR PARAMETERS, {:company_id => []})

  2. 然后您可以使用以下方式调用它:

    params["user"]["company_id"]

  3. 我认为您可以在更新操作中执行此操作:

    @company = Company.find(params["user"]["company_id"])

  4. 以下是我的工作示例表单:

    <%= form_for :user, :url  => custom_users_path do |f| %>
        <%= f.text_field :name %>
        <%= f.hidden_field :company_id, :value => Company.find(1).id %> #4
    <%= f.submit "Submit" %>
    

    我提交后,在我的custom action我的params["user"]["company_id"]内会有此值:4