Frage

My slim template looks like this:

= form_for @user_event do |f|
  .field
      = f.select :invited, @friendsArray, {}, { :multiple => true }
  .actions = f.submit 'Save'

@friendsArray example = [["John Nash", "8360775"], ["Jonathan Baldy", "30880087"], ["David Gray", "39900933"]]

When I remove { :multiple => true } it submits an id for the chosen name.

When I add { :multiple => true } I want it to submit multiple id's but it just returns {} for the user_event_params.

War es hilfreich?

Lösung

You must permit an array [] in your controllers params method.

In the user_events_controller.rb I now have

def user_event_params params.require(:user_event).permit(invited: []) end

You then may get an error as your database probably won't allow you to save an array to it.

This can be fixed my adding serialize :invited to your model, in my case user_event.rb. This is cool because when you retrieve the attribute from the db in turns it from a string back to an array.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top