首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails强参数TypeError无符号到整数的隐式转换

Rails强参数TypeError无符号到整数的隐式转换
EN

Stack Overflow用户
提问于 2018-05-30 21:30:57
回答 2查看 719关注 0票数 0

为什么会出现此错误?:

TypeError (不隐式将符号转换为整数)

在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"} ] } ] }}

但是我如何在post表单中实现这一点呢?

EN

回答 2

Stack Overflow用户

发布于 2018-05-30 21:53:48

因为missives_attributes包含content

对于missives_attributes,您应该使用<%= fff.text_area :content %>,而不是<%= f.text_area :content %>

<%= 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 %>
票数 1
EN

Stack Overflow用户

发布于 2018-06-04 05:17:08

我终于让它工作了:

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一起使用,但是一些Stack Overflow的答案说它也应该和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)斯蒂芬·维尔是对的,missives_attributes必须以数组的形式提交。感谢Kartikey Tanna为this解答了如何实现这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50606032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档