[Vuejs]-Vue.js not persisting select box info in Rails app

0👍

Summarising based on the comments and your own fix with some additional info that may help future users.

  • Confirm if v-model is present in the HTML
    • View Source
    • Do not Inpect elements using Dev Tools as this will not show v-model
  • If v-model is not in the source, there is most likely a problem with the formatting of the select method
    • Format: select(method, choices = nil, options = {}, html_options = {}, &block) API docs
    • Because options and html_options both accept a hash, it’s safer to explicitly include both so Ruby knows how to parse it (even if that means including an empty hash).
### Correct examples

# multi-line format for readability
# select(
#  method, 
#  choices = nil, 
#  options = {}, 
#  html_options = {}
# )

# no `options` provided, but explicit empty `options` hash '{}' ('The fix' in the original post)
<%= f.select(
  :property_type, # method
  options_for_select(@types.map {|type| [type.titleize, type]}, listing.property_type), # choices
  {}, # options
  {'v-model': 'propertyType'} # html_options
) %>

# with `options` values
<%= f.select(
  :property_type, # method
  options_for_select(@types.map {|type| [type.titleize, type]}, listing.property_type), #choices
  {include_blank: 'Select your type...'}, # options
  {'v-model': 'propertyType'} # html_options
) %>

Leave a comment