にネストされた属性の強力なパラメータを許可します新しいメモの作成は次のとおりです。正しい方法は、私は次のようにnote.rbという名前のモデルを持っているRailsの
<%= 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>
メモのレコードを持つタグのレコードを作成しようとしています。私は
def create
@note = Note.new(note_params)
@note.user = current_user
if @note.save
redirect_to '/notes'
else
render 'new'
end
end
と持っている私のnotes_controller.rbで
:
TypeError (no implicit conversion of Symbol into Integer):
私ならば、私は同じエラーを取得します。フォーム上
今private
def note_params
params.require(:note).permit(:title, :description, tags_attributes: [:id, :name])
end
は、私は次のことを得る提出します使用:
params.require(:note).permit(:title, :description, :tags_attributes => [:id, :name])
私はエラーを取得する:
Unpermitted parameter: :tags_attributes
私が使用している場合:フォームの
params.require(:note).permit(:title, :description, :tags_attributes => [])
のparams提出:
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を使用する。
申し訳ありませんが、それをチェックアウトほとんどであるブログがあり
:tags_attributes => [{id: "", name: ""}, {id: "", name: ""}]
のような配列形式でタグを送信するためのフロントエンドを変更してください私はまだ自分自身を学んでいます。私はとにかく助けることを願って – jj56723
試してみました。それは動作しませんでした – user122121
あなたは私にスタックトレースと完全なエラーメッセージを与えることはできますか?と行番号? – jj56723