【问题标题】:Rails strong params TypeError no implicit conversion of Symbol into IntegerRails 强参数 TypeError 没有将 Symbol 隐式转换为 Integer
【发布时间】:2018-06-04 21:10:25
【问题描述】:

为什么会出现这个错误?:

TypeError(没有将 Symbol 隐式转换为 Integer)

在posts_controller 的create 操作中:

posts_controller.rb

@post = @character.posts.create(post_params)
...

def post_params
  params.require(:post).permit( conversation_attributes: [ missives_attributes: [ :content ] ] )
end

post.rb

has_one :conversation, class_name: 'Postconversation', dependent: :destroy
accepts_nested_attributes_for :conversation

postconversation.rb

has_many :missives, class_name: 'Postmissive', dependent: :destroy
accepts_nested_attributes_for :missives

_post_form.html.erb

<%= f.fields_for :conversation_attributes do |ff| %>
  <%= ff.fields_for :missives_attributes do |fff| %>
    <%= hidden_field_tag :callsign, character.callsign %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

日志

Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=>{"missives_attributes"=>{"content"=>"Hello"}}}}

编辑

根据这个问题,看起来我需要在我提交的参数周围加上方括号: Strong parameters with has_many gives "no implicit conversion of Symbol into Integer"

Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=> [ {"missives_attributes"=> [ {"content"=>"Hello"} ] } ] }}

但是我如何在帖子表单中实现这一点??

【问题讨论】:

  • 这个&lt;%= f.text_area :content %&gt;不应该是&lt;%= fff.text_area :content %&gt;
  • 只是猜测:missives 是一个 has_many 关系,因此在您的参数中它被视为一个数组,:content 作为数组索引(它应该是一个整数而不是符号)。
  • 不应该是f.fields_for :conversationff.fields_for :missives,没有_attributes吗?
  • 这就是我最初的想法,但其他 SO 的答案似乎并非如此。没有_attributes,我得到这个错误:Unpermitted parameter :conversation
  • 您的 form_for(或 form_with)中有什么?您是为@post 定义它还是为 :post 定义它?

标签: ruby-on-rails


【解决方案1】:

因为,missives_attributes 包含 content

你应该使用 &lt;%= fff.text_area :content %&gt; 代替 missives_attributes 而不是 &lt;%= f.text_area :content %&gt;

<%= f.fields_for :conversation_attributes do |ff| %>
  <%= ff.fields_for :missives_attributes do |fff| %>
    <%= hidden_field_tag :callsign, character.callsign %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

【讨论】:

  • 是的,你说的很对,我已经纠正了这个错误,但原来的 TypeError 仍然存在。
  • 你能粘贴更多的错误`和contoller方法cmng
  • 没有解决,我越来越绝望了,我实际上正在考虑提供赏金的历史无用策略。
  • 在哪个方法上 cmng,createshow?你能检查一下它是cmng的地方吗
  • 它在create 方法中。在@post = @character.posts.create(post_params) 行之前没有什么特别的事情发生。我很困惑。
【解决方案2】:

终于搞定了:

posts_controller.rb

@post = @character.posts.create(post_params)
...

def post_params
  params.require(:post).permit( conversation_attributes: [ missives_attributes: [ :content ] ] )
end

post.rb

has_one :conversation, class_name: 'Postconversation', dependent: :destroy, inverse_of: :post
accepts_nested_attributes_for :conversation

postconversation.rb

belongs_to :post, inverse_of: :conversation
has_many :missives, class_name: 'Postmissive', dependent: :destroy, foreign_key: 'conversation_id', inverse_of: :conversation
accepts_nested_attributes_for :missives

postmissive.rb

belongs_to :conversation, class_name: 'Postconversation', foreign_key: 'conversation_id', inverse_of: :missives
validates :conversation, presence: true # validates :conversation_id does not work

_post_form.html.erb

<%= f.fields_for :conversation_attributes do |ff| %>
  <%= ff.fields_for 'missives_attributes[]', Postmissive.new do |fff| %>
    <%= hidden_field_tag :callsign, character.callsign %>
    <%= fff.text_area :content %>
    ...
  <% end %>
<% end %>

提交的参数

Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxHD...VoA==", "callsign"=>"baz", "post"=>{"conversation_attributes"=>{"missives_attributes"=>[{"content"=>"Hello"}]}}}

备注

1) 注意添加了inverse_of:,以便Rails 识别双向关联。没有这个,数据库保存失败。文档说它只需要与has_onehas_many 一起使用,但一些堆栈溢出答案说它也应该与belongs_to 一起使用,所以这就是我所做的(belongs_to :post, inverse_of: :conversationbelongs_to :conversation, inverse_of: :missives) .欢迎进一步了解这一点。

2) post_params 没问题。无需添加任何括号。

3) 在postmissive.rb 中,验证必须是validates :conversation,而不是validates :conversation_id

4) Stephan Wiehr 是对的,missives_attributes 必须作为数组提交。感谢 Kartikey Tanna this 回答如何实现这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多