0
もう1つの新しい質問です。バンドhas_manyアルバム。has_many build AssociationTypeMismatchエラー
ActiveRecord::AssociationTypeMismatch (Band(#70076964285020) expected, got "1" which is an instance of String(#12787800)):
app/controllers/albums_controller.rb:20:in `create'
...
アルバムコントローラ
def new
binding.pry
@band = Band.find(params[:band])
authorize @band, :admin?
@album = Album.new
respond_to do |format|
format.js
end
end
def create
binding.pry
@band = Band.find(params[:album][:band].to_i)
authorize @band, :admin?
@album = @band.albums.build(album_params)
if @album.save
@albums = @band.albums
#@eps = @band.eps
#songs = @band.songs
respond_to do |format|
format.js
end
else
@fail = "fail"
respond_to do |format|
format.js
end
end
end
def album_params
params.require(:album).permit(:band, :album_name, :album_release_date, :etc)
end
形#createでビルドラインである:私はエラーになっています
<%=simple_form_for(@album, remote: true, :authenticity_token => true, format: :js) do |f| %>
<%= f.hidden_field :band, :value => @band.id %>
<%= f.input :album_name %>
<%= f.input :album_release_date %>
<%= f.input :etc %>
<div id="albumsubmit">
<div class="form-actions">
<%= f.button :submit, "Create Album", class: "btn btn-primary" %>
</div>
</div>
スキーマを
create_table "albums", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "band_id"
t.string "album_name"
t.string "album_release_date"
t.index ["band_id"], name: "index_albums_on_band_id"
end
'@band = Band.find(のparams [:アルバム] [:バンド] .to_i)' :) –
右側に、感謝します。私はそれを10分前に解消しました。つまり、パラメータからバンドを取り除くと、何かがアソシエーションと混ざっていて、band_idフィールドがビルドから自動的に帰属するようです。 – fdsaevad