Closed jvanus closed 6 years ago
Thanks @jvanus
I think you need to write your .erb so that div
contains input
like the code below.
<div class="ui-sortable">
<div id="Question_1" class="ui-sortable-handle">
<input type="hidden" value="1" id="survey_questions_attributes_0_id" name="survey[questions_attributes][0][id]">
...
</div>
</div>
In addition, unfortunately RailsSortable only works well with persisted model for now. If you are trying to put it into form
to persist some models, please just use jquery.sortable
instead, and sort them with the param's sequence.
Yeah, thats what I did for my workaround above. All the models being sorted are persisted, they are just also editable. My proposal is really just to make the rails_sortable controller a bit more robust by removing the assumption that no extraneous data could be included in params.
You have a point. The rails_sortable controller is on poor implementation as you say. I will improve it some time.
This issue defines a narrow case as I ran into it. The proposed fixes try to assume there are other scenarios that could cause the same problem. I think it would be fair to classify this as a feature request rather than a bug, as the problematic html does not conform to the example in
README.md
. I think this may be a somewhat common use case however so its probably worth 'fixing'. I would be happy to work on a patch for this, but I'd like to know your thoughts before I do.Problem:
Rails automatically adds hidden fields after form_for or fields_for which may be at the same level rails_sortable is looking for children to submit.
For example: survey.rb
surveys/_form.html.erb
Will produce html like:
Stack Trace:
In this case
parse_params
is expecting the params hash to only have the keyQuestions
.Possible Fixes:
User Workaround (no changes to rails_sortable)
include_id: false
<%= f.hidden_field :id %>
Front end fix
$(this).sortable('serialize')
with something like$(this).find('.a-class-selector').sortable('serialize')
Back end fix
ApplicationRecord.descendants.select{ |c| c.included_modules.include?(RailsSortable::Model) }