On my site you can open sessions, and add instruments to the session. An admin (the person who creates the session) can choose what instruments are in the session.
In this example we will say "The admin wants the following instruments: 1, 1 again and instrument 2". So far everything proceed as expected but then... #{start.exciting.music.now}
The admin wants to add another instrument but if the instrument already exist you can't add it again?
Edit form
.col-xs-12.item
%hr
%ul#instrument-selection.instrument-list
= link_to instruments_path, class: 'fancybox' do
%li
= image_tag('plus.png', class: 'fit')
Add instrument
- @meeting.instruments.each do |instrument|
%li
= image_tag(instrument.icon_path)
= instrument.name
%input{type: 'hidden', value: instrument.id, name: 'meeting[instrument_ids][]'}
%i.fa.fa-times.delete-me
What you open when you click on the fancy box link
%section#musician-listing
.col-xs-12.item
.musician-badge
%ul.musician-info
- @instruments.each do |instrument|
%li.col-xs-12.item
.col-xs-6
= image_tag(instrument.icon_path, class: 'fit instrument-icons')
.col-xs-6
= link_to '#', class: 'btn btn-success fit square add-instrument', data: {instrument_id: instrument.id, name: instrument.name, icon: "/#{instrument.icon_path}"} do
Add
= instrument.name
.clearboth
Meeting controller
def create
meeting = current_user.meetings.create( meeting_params )
if meeting.valid?
redirect_to meeting
flash[:notice] = "Succesfully created session"
else
redirect_to new_meeting_path
flash[:alert] = "Oops.. You haven't filled in the required information"
end
end
def update
meeting = current_user.meetings.find(params[:id])
meeting.update_attributes(meeting_params)
redirect_to meeting
flash[:notice] = "Succesfully edited session"
end
The output when updating the meeting (seems fine I guess)
Started PATCH "/meetings/163" for 127.0.0.1 at 2016-06-18 22:29:41 +0200
Processing by MeetingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZJvwuX3x4f7cACp5N75IOEn7zczO4qyevmxydiN+LF+AZnys11kioeAbUU/jwlc76+mAdN2c2vvZKFpgg+6vPA==", "meeting"=>{"title"=>"Test2", "closed_session"=>"false", "genre_id"=>"2", "instrument_ids"=>["1", "1", "1", "1", "2"], "user_ids"=>["2"]}, "commit"=>"Update session", "id"=>"163"}
Now the weird part!
Even if the "instrument_ids" returns ["1", "1", "1", "1", "2"] it just removes the 3th and the 4th "1" and it keeps the "2".
If we would update it again (before updating the array is ["1", "1", "2"]) and for example we add another "2" and a "5" and another "1", the PATCH command will return ["1", "1", "2", "2", "5", "1"] but it will end up with ["1", "1", "2", "5"].
Again everything what already exist is removed
If you create a new meeting you can add as much the same ids if you like.
Any help would be much appreciated it's driving me mad....
Cheers, Chris
P.S. staring at this issues for almost 12 hours... haha damn....
Aucun commentaire:
Enregistrer un commentaire