Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5 HABTM未经许可的参数_Ruby On Rails_Activerecord_Nested_Has And Belongs To Many - Fatal编程技术网

Ruby on rails Rails 5 HABTM未经许可的参数

Ruby on rails Rails 5 HABTM未经许可的参数,ruby-on-rails,activerecord,nested,has-and-belongs-to-many,Ruby On Rails,Activerecord,Nested,Has And Belongs To Many,在一天一事无成之后,我已经认输了(答案可能会非常简单,但这是代码) 好的,我有两个模型,用户(带装置)和章节 模型: 绑定。撬入控制器 绑定、撬动响应 我知道这个有点难看,但基本上参数是被提交的,但是用户[:first_name,:last_name,:email]是不被允许的 [1] 撬动(#)>章节参数 未经允许的参数::first_name,:last_name,:email=>“London”,“country_id”=>“1”}允许:true> [2] 撬动(#)>参数=>”✓", "

在一天一事无成之后,我已经认输了(答案可能会非常简单,但这是代码)

好的,我有两个模型,用户(带装置)和章节

模型: 绑定。撬入控制器 绑定、撬动响应 我知道这个有点难看,但基本上参数是被提交的,但是用户[:first_name,:last_name,:email]是不被允许的

[1] 撬动(#)>章节参数 未经允许的参数::first_name,:last_name,:email=>“London”,“country_id”=>“1”}允许:true> [2] 撬动(#)>参数=>”✓", "_方法“=>”补丁“、”真实性令牌“=>”9IdAqcAsVG5vvakSf7jHRk+WplN/GyVZUxmAdefUbTX/uI6IajmZPr1YSol21Zhzdl7P/lrl+SVnWr5mBMiljw==”、”章节“=>”伦敦“、”国家id“=>”1“、”名字“=>”Tim2“、”姓氏“=>”听说过”、“电子邮件“=>”jmcgregorx@spartaglobal.com}允许:false>,“提交”=>“更新”,“控制器”=>“章节”,“操作”=>“更新”,“id”=>“1”}允许:false>


简而言之,我希望通过表单将某些用户连接到某些章节,但我不知道如何通过章节更新/创建方法获取嵌套参数。我的头是红色的,肉质的。请提供帮助。

由于您试图修改的字段是针对子模型的,您必须告诉表单,因为默认情况下,表单假定您正在编辑父模型上的字段。实现这一点的方法是(特别是“一对多”部分)

因此,您将按照以下思路进行操作(代码下面的解释):

您需要
users\u属性
而不是
user\u属性
,因为复数“users”告诉rails需要多个子(多个用户)的属性。然后在该数组中,您希望告诉它所有预期的字段,包括
id
。即使您没有显式定义ID的字段,Rails也会设置一个字段,以允许您编辑已经存在的子项的属性(并且不仅能够创建新的子项)。文档中的片段:

注意#fields_for将自动生成一个隐藏字段来存储记录的ID。在某些情况下,不需要此隐藏字段,您可以传递include_id:false以防止#fields_for自动呈现它


如果这方面的问题不起作用,或者如果你有更多的问题,请随时告诉我

您好@bwalschy,首先感谢您的回复。其次,不幸的是,它没有起作用。我将我的代码更改为您的,并在您回复的第一行之后添加,以检查我是否获得了用户,我就是。但是在那之后,没有任何东西可以从form.fields\u打印出来。这与这行有关吗:form.fields|for:users,chap|u user do|user|u fields|哎呀,我在那行没有等号来表示打印表单字段。您需要:
。(在
之后有一个等号,我还更新了我的答案,以显示控制器参数应该是什么样子(以及为什么)
class User < ApplicationRecord
  has_many :projects
  has_and_belongs_to_many :chapters
end

class Chapter < ApplicationRecord
  belongs_to :country
  has_and_belongs_to_many :users
  accepts_nested_attributes_for :users
end
[...]
<% @chapter.users.each_with_index do |chap_user, i| %>
  <div class="row">
    <div class="col-md-4 col-md-offset-4  col-xs-8 col-xs-offset-2">
      <small>User <%= i + 1 %></small>
    </div>
  </div>

  <div class="row">
    <div class="col-md-2 col-md-offset-4  col-xs-8 col-xs-offset-2">

      <div class="field form-group">
        <%= form.label 'First Name' %>
        <%= form.text_field :first_name, value: chap_user.first_name,class:"form-control"%>
      </div>
    </div>
    <div class="col-md-2 col-md-offset-0 col-xs-8 col-xs-offset-2">
      <div class="field form-group">
        <%= form.label 'Last Name' %>
        <%= form.text_field :last_name, value: chap_user.last_name, class:"form-control"%>
      </div>

    </div>
  </div>
  <div class="row">
    <div class="col-md-4 col-md-offset-4 col-xs-8 col-xs-offset-2">
      <div class="field form-group">
        <%= form.label :email %>
        <%= form.text_field :email, value: chap_user.email, class:"form-control"%>
      </div>

    </div>
  </div>
<% end %>
[...]
# Never trust parameters from the scary internet, only allow the white list through.
def chapter_params
  params.fetch(:chapter, {})
  params.require(:chapter).permit(:city, :country_id, user_attributes: [:first_name,:last_name,:email])
end
# or...
params.require(:chapter).permit(:city, :country_id, user_ids: [])
# or...
params.require(:chapter).permit(:city, :country_id, user_attributes: [:id, :first_name])
# or...
params.require(:chapter).permit(:city, :country_id, users_attributes: [:id, :first_name])
  # PATCH/PUT /chapters/1
  # PATCH/PUT /chapters/1.json
  def update
    binding.pry
    respond_to do |format|
      if @chapter.update(chapter_params)
        format.html { redirect_to @chapter, notice: 'Chapter was successfully updated.' }
        format.json { render :show, status: :ok, location: @chapter }
      else
        format.html { render :edit }
        format.json { render json: @chapter.errors, status: :unprocessable_entity }
      end
    end
  end
<% @chapter.users.each_with_index do |chap_user, i| %>
  <%= form.fields_for :users, chap_user do |user_fields| %>
    <div class="row">
      <div class="col-md-4 col-md-offset-4  col-xs-8 col-xs-offset-2">
        <small>User <%= i+ 1 %></small>
      </div>
    </div>

    <div class="row">
      <div class="col-md-2 col-md-offset-4  col-xs-8 col-xs-offset-2">
        <div class="field form-group">
          <%= user_fields.label 'First Name' %>
          <%= user_fields.text_field :first_name, value: chap_user.first_name,class:"form-control"%>
        </div>
      </div>
      <div class="col-md-2 col-md-offset-0 col-xs-8 col-xs-offset-2">
        <div class="field form-group">
          <%= user_fields.label 'Last Name' %>
          <%= user_fields.text_field :last_name, value: chap_user.last_name, class:"form-control"%>
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col-md-4 col-md-offset-4 col-xs-8 col-xs-offset-2">
        <div class="field form-group">
          <%= user_fields.label :email %>
          <%= user_fields.text_field :email, value: chap_user.email, class:"form-control"%>
        </div>
      </div>
    </div>
  <% end %>
<% end %>
params.require(:chapter).permit(:city, :country_id, users_attributes: [:id, :first_name, :last_name, :email])