另一个新问题。乐队has_many专辑。我收到了错误:
ActiveRecord :: AssociationTypeMismatch(乐队(#70076964285020)预期, 得到" 1"这是String的一个实例(#12787800)):
中
app / controllers / albums_controller.rb:20:在'create'
...这是#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
形式:
<%=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
答案 0 :(得分:0)
当你传递给具有has_many关联的保存方法参数时,他希望&#34; band&#34;的实例,只需将参数名称设置为 band_id
这里:
<%= f.hidden_field :band, :value => @band.id %>
为:
<%= f.hidden_field :band_id, :value => @band.id %>
在这里:
params.require(:album).permit(:band, :album_name, :album_release_date, :etc)
为:
params.require(:album).permit(:band_id, :album_name, :album_release_date, :etc)