【发布时间】:2017-10-15 09:34:18
【问题描述】:
我的模型名为 note.rb 如下:
class Note < ApplicationRecord
belongs_to :user
has_many :tags
accepts_nested_attributes_for :tags
end
还有一个名为tag.rb的模型:
class Tag < ApplicationRecord
belongs_to :note
end
新建笔记的形式如下:
<%= form_with scope: :note, url: notes_path, local: true do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br>
<%= f.text_area :description %>
</p>
<%= f.fields_for :tags_attributes do |t| %>
<p>
<%= label_tag(:name, "Add a tag") %><br>
<%= t.text_field :name %>
</p>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
<div id= "tag-displayer">
<span id= "tags"></span>
</div>
我正在尝试为带有注释记录的标签创建记录。
在我的 notes_controller.rb 我有
def create
@note = Note.new(note_params)
@note.user = current_user
if @note.save
redirect_to '/notes'
else
render 'new'
end
end
和:
private
def note_params
params.require(:note).permit(:title, :description, tags_attributes: [:id, :name])
end
现在在提交表单时,我得到以下信息:
TypeError (no implicit conversion of Symbol into Integer):
如果我使用,我会得到同样的错误:
params.require(:note).permit(:title, :description, :tags_attributes => [:id, :name])
我得到错误:
Unpermitted parameter: :tags_attributes
如果我使用:
params.require(:note).permit(:title, :description, :tags_attributes => [])
表单提交参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iSsLyOb0ZxZP0rfB4I5yfyrw965zJSLrtkroTUzseY2k4o5DwKpKXlyxN6p99pt4Fwju1RhMZPkbNdv+YVSESQ==", "note"=>{"title"=>"test note with Tag", "description"=>"Test note with tag", "tags_attributes"=>{"name"=>"Rails"}}, "commit"=>"Save Note"}
我不确定自己做错了什么,我已经尝试了所有可能的解决方案。
在 ruby 2.4.1 中使用 Rails 5。
【问题讨论】:
标签: ruby-on-rails ruby nested-attributes strong-parameters