Reputation: 1113
I've got working new/create actions and a form for my @miniature
model and it's nested model @scales
. I can't get the update/edit actions right. Should be simple but I'm very stuck.
@miniature
has_many @scales
through @sizes
.
In my @miniature
model I have
has_many :sizes, dependent: :destroy
has_many :scales, :through => :sizes
accepts_nested_attributes_for :sizes, allow_destroy: true
In the controller I have
def new
@miniature = Miniature.new
@all_scales = Scale.all
@size = @miniature.sizes.build
end
def create
@miniature = Miniature.new(miniature_params)
params[:scales][:id].each do |scale|
if !scale.empty?
@miniature.sizes.build(:scale_id => scale)
end
end
if @miniature.save
redirect_to miniature
else
render 'new'
end
end
private
def miniature_params
params.require(:miniature).permit(:name, :release_date, :material, :pcode, :notes, sizes_attributes: [:id, :scale_id, :miniset_id])
end
That works but edit and update actions do not. I have my edit set up the same as my new.
def edit
@miniature = Miniature.find(params[:id])
@all_scales = Scale.all
@size = @miniature.sizes.build
end
I had thought that updating miniature params
would update the @sizes
model but it doesn't
def update
@miniature = Miniature.find(params[:id])
if @miniature.update_attributes(miniature_params)
flash[:success] = "Miniature updated"
redirect_to @miniature
else
render 'edit'
end
end
It currently updates the @miniature
but not the @sizes
info. Is my problem in the edit or in the update or in both?
The relevant bit of the form:
<%= f.fields_for(@size) do |sf| %>
<%= sf.label simple_pluralize(@miniature.scales.count, 'Scale') %>
<%= collection_select( :scales, :id, @all_scales, :id, :name,
{:selected => @miniature.scales.map(&:id)},
{class: 'multiselect', multiple: true}) %>
<% end %>
Any help or pointers to further reading very much appreciated. Even if it's just to say "You're overlooking this obvious thing, go and do some more reading/work".
It seems likely I need to have an update
action more similar to my create
action's if
statement?
UPDATE for JKen13579
This is the PATCH request from my server log when submitting an edit:
Started PATCH "/miniatures/21" for 127.0.0.1 at 2014-04-02 16:00:10 +0100
Processing by MiniaturesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jQ79L1Exx83C47jnCF3nsWQ2tV07tRwKfI8wNeLzojo=", "miniature"=>{"name"=>"Test Miniature", "material"=>"Metal", "pcode"=>"123123123123123", "release_date(1i)"=>"2013", "release_date(2i)"=>"2", "release_date(3i)"=>"2", "notes"=>""}, "scales"=>{"id"=>["", "2"]}, "commit"=>"Save changes", "id"=>"21"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 4 ORDER BY "users"."id" ASC LIMIT 1
Miniature Load (0.2ms) SELECT "miniatures".* FROM "miniatures" WHERE "miniatures"."id" = ? LIMIT 1 [["id", "21"]]
(0.1ms) begin transaction
(0.2ms) commit transaction
Redirected to http://localhost:3000/miniatures/21
Completed 302 Found in 12ms (ActiveRecord: 1.3ms)
UPDATE for Observer
I'm using fields_for because that's what I managed to get the new/create action to work with. I'm not tied to it if there is a better way, at least as far as I know.
My routes has
resources :miniatures do
collection do
get :scales
get :collections
get :lines
get :contents
post :import
end
member do
get :minisets, :setminis
end
end
and further down
resources :sizes
resources :scales
I think my routes file is generally a bit cluttered and is due for a refactoring.
Upvotes: 3
Views: 2801
Reputation: 53038
As per the server log entry(see below), I see that in params
hash, scales
(highlighted in bold) is being passed as a separate hash and not within miniature
:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jQ79L1Exx83C47jnCF3nsWQ2tV07tRwKfI8wNeLzojo=", "miniature"=>{"name"=>"Test Miniature", "material"=>"Metal", "pcode"=>"123123123123123", "release_date(1i)"=>"2013", "release_date(2i)"=>"2", "release_date(3i)"=>"2", "notes"=>""}, "scales"=>{"id"=>["", "2"]}, "commit"=>"Save changes", "id"=>"21"}
Change the update
action as:
def update
@miniature = Miniature.find(params[:id])
if params[:scales][:id]
## Convert ["", "1","2","4","8"] to [1,2,4,8]
params[:scales][:id] = params[:scales][:id].reject(&:empty?).map(&:to_i)
## Get the scale_id from sizes already present in database [1,2,5,6]
old_scales = @miniature.sizes.pluck(:scale_id)
## Find the new scales to be added [1,2,4,8] - [1,2,5,6] = [4,8]
new_scales = params[:scales][:id] - old_scales
## Find the old_scales to be deleted [1,2,5,6] - [1,2,4,8] = [5,6]
old_scales = old_scales - params[:scales][:id]
## Build new_scales [4,8]
new_scales.each do |scale|
@miniature.sizes.build(:scale_id => scale)
end
## Delete old_scales [5,6]
Size.delete_all(:scale_id => old_scales)
end
if @miniature.update_attributes(miniature_params)
flash[:success] = "Miniature updated"
redirect_to @miniature
else
render 'edit'
end
end
Update the create
action so scale_id
is passed as Integer and not String:
def create
@miniature = Miniature.new(miniature_params)
if params[:scales][:id]
## Convert ["", "1","2","4","8"] to [1,2,4,8]
params[:scales][:id] = params[:scales][:id].reject(&:empty?).map(&:to_i)
params[:scales][:id].each do |scale|
@miniature.sizes.build(:scale_id => scale)
end
end
if @miniature.save
redirect_to miniature
else
render 'new'
end
end
Upvotes: 6