ruby on rails - form_for when updating fails, though creation works? -
i have model, disc
.
the disc#new
action passes @disc = disc.new
view has simple form_for
helper, reading
form_for(:disc, url: {action: 'create'}) |f| # usual text fields etc follow
in discs_controller.rb
, have private method disc_params
, reads params.require(:disc).permit(:owner, :title)
, on. disc#create
says disc.create(disc_params)
.
that works totally fine. run trouble creating edit form.
disc#edit
reads @disc = disc.find(params[:id])
. edit
view exact clone of new
view, opening line reads form_for(:disc, url: {action: 'update', id: @disc.id})
. submits disc#update
, reads disc.find(params[:id]).update(disc_params)
.
this seems me should work, evidently i'm wrong, throws following error: param missing or value empty: disc.
the trace points these 2 lines:
params.require(:disc).permit(:owner_id, :title, :contents)
and
disc.find(params[:id]).update(disc_params)
this one's stumping me. i'd appreciate if clarify why doesn't seem work.
instead of using this:
form_for(:disc, url: {action: 'create'}) |f| form_for(:disc, url: {action: 'update', id: @disc.id}) |f|
just provide instance of disc
model:
form_for @disc |f|
it surely should eliminate errors (generate correct url , method) , gives opportunity use same form new
, edit
actions.
Comments
Post a Comment