Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3917

inheritance + nested_attributes + strong_params

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I have the following code skeleton where a Column embeds_many Contents of types Image and Map:

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Column
        include Mongoid::Document
      
        embeds_many: contents
        accepts_nested_attributes_for :contents, allow_destroy: true
      end
      
      class content
        include Mongoid::Document
      
        field :description
      end
      
      class Image < Content
        field :src, type: String
      end
      
      class Map < Content      
        field :latitude, type: String
        field :longitude, type: String
      end
      
      

      `
      Now I want to pass a JSON from my view to the columns_controller to create a column with two contents; image and map.

      I attempted passing the following hash:

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      {'column' => 'contents_attributes' => [{_type: 'Image', description: 'image description', src: 'path/to/image'}, {_type: 'Map', description: 'map description', latitude: '25.3321', longitude: '35.32423'}]}
      

      `
      the column_params method is:

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      
      def column_params
        params.require(:column).permit(:_id, contents_attributes: [:_id, :_type, :description, :src, :latitude, :longitude]) 
      end
      
      

      `

      The above raised the following error:

      Unable to find source-code formatter for language: `. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Attempted to set a value for '_type' which is not allowed on the model Content.
      

      `

      and if I manually add the _type field to the Content class, it also raise while permitting the subclasses' attributes

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            ElBayaa ElBayaa [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: