has_many_and_belongs_to:param到:param

时间:2019-01-10 23:57:21

标签: ruby-on-rails ruby

我无法在新模型中输入数据。

我创建了一个新模型,以便可以将用户分配给关联公司。

我有一个Users,Affiliates和UserAffiliates模型。

UserAffiliates

  belongs_to :users
  belongs_to :affiliates

用户

  has_and_belongs_to_many :user_affiliate
  has_and_belongs_to_many :affiliate, through: :user_affiliate, optional: true

会员

  has_many :user_affiliates
  has_many :users, through: :user_affiliates

它是如何工作的,一个会员可以有很多用户,但是一个用户永远只有一个会员。然后,我在UserAffiliates表中有一个佣金列,以根据每个用户设置佣金。

我创建了此表单,但它似乎不起作用(这是由于<% @users.each do |user| %>遍历表中的每个用户所致:

<%= form_for user, remote: true do |f| %>
  <% f.fields_for :user_affiliates,  remote: true do |f| %>
    <%= f.text_field :affiliate_id, class: "form-control"  %>
  <%= f.submit "Add Affiliate", data: {confirm: "Are you sure?"}, class: "btn btn-light" %>
  <% end %>
<% end %>

我收到此错误:

Parameters: {"utf8"=>"✓", "user"=>{"user_affiliates"=>{"affiliate_id"=>"1"}}, "commit"=>"Add Affiliate", "id"=>"2"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:58
Completed 500 Internal Server Error in 62ms (ActiveRecord: 6.4ms)



ArgumentError (wrong number of arguments (given 0, expected 1..2)):

app/controllers/users_controller.rb:88:in `[]'
app/controllers/users_controller.rb:88:in `user_params'
app/controllers/users_controller.rb:61:in `block in update'
app/controllers/users_controller.rb:60:in `update'

用户控制器参数

 def user_params
    params.require(:user).permit(:name, :approved, :seller, :buyer, :admin, :stripe_account, :email, :password, :password_confirmation, :role, :affiliate_id, :affiliate_ids [], :user_affiliates, :commission)
 end

我的表格有误吗?

我希望能够将表中的用户列出为user.admin,具有一个文本字段,然后输入affiliate_id,然后将user_id和affiliate_id输入到UserAffiliate表中。我还希望能够添加user.admin和Affiliate都可以更改的佣金率。

如何正确执行此操作?

更新

我已修正参数,但仍然存在错误:

参数:

def user_params
       params.require(:user).permit(:name, :approved, :seller, :buyer, :admin, :stripe_account, :email, :password, :password_confirmation, :role, :affiliate_id, :user_affiliates, :affiliate, :commission, affiliate_ids: [])
end

错误:

Started PATCH "/users/2" for 127.0.0.1 at 2019-01-10 21:20:53 -0500
Processing by UsersController#update as JS
  Parameters: {"utf8"=>"✓", "user"=>{"user_affiliates"=>{"affiliate_id"=>"1"}}, "commit"=>"Add Affiliate", "id"=>"2"}
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:58
Unpermitted parameter: :user_affiliates
   (0.5ms)  BEGIN
  ↳ app/controllers/users_controller.rb:61
   (0.1ms)  ROLLBACK
  ↳ app/controllers/users_controller.rb:61
Completed 406 Not Acceptable in 21ms (ActiveRecord: 5.3ms)



ActionController::UnknownFormat (ActionController::UnknownFormat):

app/controllers/users_controller.rb:60:in `update'

我试图在所有控制器,模型和形式中将其更改为user_affiliate(非脉冲)和复数形式。我需要对用户控制器上的更新进行任何操作吗?

用户控制器更新:

  def update
   @user = User.find(params[:id])
   # @affiliate = current_affiliate
   respond_to do |format|
     if @user.update(user_params) # <- you'll need to define these somewhere as well
       if @user.admin?
         format.html { redirect_to '/admin/users', notice: "yahoo" }
         format.json { render json: @user }
       # elsif @affiliate
       #     format.html { redirect_to '/a/clients', notice: "yahoo" }
       #     format.json { render json: @user }
       else
         format.html { render :edit }
         format.json { render json: { errors: @user.errors }, status: :unprocessable_entity }
       end
     end
   end

0 个答案:

没有答案