title: Rails 5.1 - How nested 2 models inside form for - between a normal table and a polymorphic one?
body: <h3>My target is to create a project instance and also its nested attributes for credits (polymorphic table) inside one single form.</h3>
  <blockquote>
  <p>⚠️ &quot;error param is missing or the value is empty: credit&quot; ⚠️</p>
  </blockquote>
  <p>I can't find the correct syntax to provide the instance to the
  polymorphic table. The problem is inside my controller I guess
  Project(action create &amp; new).</p>
  <h3> When I test my code with the console everything seams to be fine. (I add the copy of that test below.)</h3>
  <p>But when I'm starting to do that within my view I have some issues and</p>
  <p>I can't figure out the right syntax.</p>
  <p> =&gt; to create the credit instance through the project form</p>
  <p> =&gt; And for sure I can't display and update those instance</p>
  <p></p>
  <h2>1 - Let's say that you have 3 models.</h2>
  <pre><code>    Project has_many credits, as creditable
      Profile has_many credits, as creditable
      Credit belongs_to :creditable, polymorphic: true
  
      # in this post we will focus on a form which  include @credit 
      # inside a @project form
      # so I let the @profile for now but I will use it later 
  </code></pre>
  <h3>routes</h3>
  <pre><code>    resources :projects do
         resources :credits, only: [:create, :edit, :update, :destroy]
      end
  </code></pre>
  For instance  let's say that I would like to display a single form for @project which include field for some credit attributes (like content, link, category).
  <p>My objective it to create that project instance and also its nested attributes for credits.</p>
  <p>=&gt; the form is inside Project_new  ProjectController action new /create...</p>
  <pre><code>    &lt;%= simple_form_for @project do |f| %&gt;
          &lt;div class=&quot;content-form&quot;&gt;
            &lt;div class=&quot;form-inputs&quot;&gt;
              &lt;%= f.simple_fields_for :credit do |credit_form| %&gt;
                 &lt;%= credit_form.hidden_field :creditable_type, :value =&gt; @creditable_type %&gt;
                 &lt;%= credit_form.input :category %&gt;
                 &lt;%= credit_form.input :content %&gt;
                 &lt;%= credit_form.input :link %&gt;
              &lt;% end %&gt;
              &lt;%= f.input :name, label: false, placeholder: &quot;nom du project&quot; %&gt;
              &lt;%= f.input :year, label: false, placeholder: &quot;Année de création&quot; %&gt;
              &lt;%= f.input :description, label: false, placeholder: &quot;Une description&quot; %&gt;
              &lt;label class=&quot;super-label&quot;&gt;
                &lt;%= f.input :mainphoto, input_html: {preview_version: :thumb}, label: false, id: &quot;over-btn&quot;  %&gt;
                &lt;%= f.attachinary_file_field :photos, as: :attachinary, cloudinary: {use_filename: true, unique_filename: false},  label: false, id: &quot;over-btn&quot; %&gt;
                &lt;div class=&quot;form-actionss&quot;&gt;
                  &lt;%= f.button :submit, class: &quot;btn-main-romain&quot; %&gt;
                &lt;/div&gt;
              &lt;/label&gt;
              &lt;p id=&quot;filename hidden&quot;&gt;&lt;/p&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;% end %&gt;
  </code></pre>
  <h2>2 - Exact copy from current models</h2>
  <pre><code>    class Profile &lt; ApplicationRecord
       belongs_to :user
       has_many :credits, :as =&gt; {:creditable =&gt; 'profile'}, :dependent =&gt; :destroy, autosave: true
       has_attachment :photo
      end
  
     class Project &lt; ApplicationRecord
      #validates :name, presence:true
      #validates :year, presence:true
      #validates :description, presence:true
      has_many :credits, :as =&gt; {:creditable =&gt; 'project'}, :dependent =&gt; :destroy, autosave: true
      attr_accessor :link
      attr_accessor :category
      attr_accessor :content
      accepts_nested_attributes_for :credits
  
      has_attachments :photos, maximum: 10
      mount_uploader :mainphoto, PhotoUploader
     end
  
     class Credit &lt; ApplicationRecord
      belongs_to :creditable, polymorphic: true
     end
  </code></pre>
  <h2>3 - Exact copy from current schema</h2>
  <pre><code>   create_table &quot;credits&quot;, force: :cascade do |t|
      t.string &quot;category&quot;
      t.string &quot;content&quot;
      t.string &quot;link&quot;
      t.string &quot;creditable_type&quot;
      t.integer &quot;creditable_id&quot;
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
     end
  
     create_table &quot;projects&quot;, force: :cascade do |t|
      t.string &quot;name&quot;
      t.integer &quot;year&quot;
      t.text &quot;description&quot;
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.string &quot;place&quot;
      t.string &quot;mainphoto&quot;
     end
  
     create_table &quot;profiles&quot;, force: :cascade do |t|
      t.bigint &quot;user_id&quot;
      t.string &quot;photo&quot;
      t.string &quot;cell_phone&quot;
      t.string &quot;city&quot;
      t.string &quot;email&quot;
      t.text &quot;description&quot;
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.string &quot;first_name&quot;
      t.string &quot;last_name&quot;
      t.integer &quot;zip_code&quot;
      t.string &quot;street&quot;
      t.string &quot;coutry&quot;
      t.index [&quot;user_id&quot;], name: &quot;index_profiles_on_user_id&quot;
     end
  </code></pre>
  <h2>4 - Crash Test inside the console</h2>
  <p>=&gt; Everything seams to be fine.</p>
  <pre><code>   #create an instance of project
     [1] Project.create(name:'project1')
      =&gt; #&lt;Project:0x007fb3d7a64e78
     id: 15,
     name: &quot;project1&quot;,
     year: nil,
     description: nil,
     created_at: Sun, 04 Feb 2018 12:09:29 UTC +00:00,
     updated_at: Sun, 04 Feb 2018 12:09:29 UTC +00:00,
     place: nil,
     mainphoto: nil&gt;
  
     #put that instance of project inside a variable called &quot;p&quot;
     [2]  p =  Project.last
      ...
  
     #create an instance of credit with an instance of project (&quot;p&quot;) as 
     attribute
     [3]  Credit.create(category:'webmaster', creditable:p)
  
     =&gt; #&lt;Credit:0x007fb3d0f9e4e0
     id: 1,
     category: &quot;webmaster&quot;,
     content: nil,
     link: nil,
     creditable_type: &quot;Project&quot;,
     creditable_id: 15,
  
  
     # check if it's possible to pick up back the project instance
     [5] Credit.last.creditable
  
     =&gt; #&lt;Project:0x007fb3d0e827c8
     id: 15,
     name: &quot;project1&quot;,
     year: nil,
     description: nil,
     created_at: Sun, 04 Feb 2018 12:09:29 UTC +00:00,
     updated_at: Sun, 04 Feb 2018 12:09:29 UTC +00:00,
     place: nil,
     mainphoto: nil
  </code></pre>
  <h2>5 - So what is the matter?</h2>
  It's not working yet inside my app view, controller
  <h2>6 - Investigate the project controller</h2>
  <pre><code>  class ProjectsController &lt; ApplicationController
      skip_before_action :authenticate_user!, only: [:index]
  
      before_action :set_project, only:[:show, :edit, :update, :destroy]
  
      layout &quot;application&quot;, only: [:index, :destroy,:show,]
      layout &quot;devise&quot;, only: [ :create, :new, :edit, :update ]
  
      def index
        @projects = Project.all
        @profile = User.find(1).profile
      end
  
  
      def show
      end
  
      def new
        @project = Project.new
        @credit = Credit.new
        @creditable_type = &quot;project&quot;
      end
  
      def create
        @project = Project.create(project_params)
        @credit = Credit.create(credit_params) #syntax pblm here
  
        creditable = params[&quot;project&quot;][&quot;credit&quot;][&quot;creditable_type&quot;]
  
        #category = params[&quot;project&quot;][&quot;credit&quot;][&quot;category&quot;]
        #link = params[&quot;project&quot;][&quot;credit&quot;][&quot;link&quot;]
        #content = params[&quot;project&quot;][&quot;credit&quot;][&quot;content&quot;]
        #allcreditables = {creditable,category,link,content}
        #@project.credits.create(allcreditables)
        
        
  
        #something like ...
        #@credit = @project.credits.create...
        # give me an error like ERROR &quot;no implicit conversion of symbol 
        # into string&quot;
  
        if @project.save
          redirect_to @project
        else
          render :new
        end
      end
  
      def edit
      end
  
      def update
        @project.update(project_params)
        if @project.save
          redirect_to @project
        else
          render :edit
        end
      end
  
      def destroy
        @project.destroy
        redirect_to projects_path
        flash[:notice] = &quot;Le project - #{@project.name} -  à bien été supprimé&quot;
      end
  
  
      private
  
      def set_project
        @project = Project.find(params[:id])
        @projects = Project.all
        @profile = User.find(1).profile
      end
  
      def credit_params
        params.require(:credit)
        .permit(:link,
          :category,
          :content,
          :creditable_type)
        .merge(project: @project)
      end
  
      def project_params
        params.require(:project).permit(
          :name,
          :year,
          :place,
          :description,
          :mainphoto,
          photos: [],
          :credits_attributes =&gt; [:link,:category,:content])
      end
    end
  </code></pre>
  <p>⚠️  the problem comes form the new and create method, I tried to do something with credit_params, but at the end @project is recorded but @Project.credits is not. I can't get the trick. Credits_attributes are inside a hash and hard for me to figure out back the way to pick them.</p>
  <h2>7 - Investigate the params that I have when I submit the form inside my view/projects/Project_new</h2>
  <pre><code>    {
        &quot;utf8&quot;=&gt;&quot;✓&quot;, &quot;authenticity_token&quot;=&gt;&quot;WKAiVi3xbzi8GpcISADZb2e3GvEut230QtmWnfPdEpFqz9ko2kuzXxHFnjufqaMfrESr96Qg33TtvIOPuv/bQw==&quot;, &quot;project&quot;=&gt;
          {
            &quot;credit&quot;=&gt;{
              &quot;category&quot;=&gt;&quot;Photographer&quot;,
              &quot;content&quot;=&gt;&quot;These photos have been taken by Martin Polak, Main photographer at ECAL, DesignSchool, Lauzane&quot;,
              &quot;link&quot;=&gt;&quot;www.martinpolak.com&quot;
              },
            &quot;name&quot;=&gt;&quot;Adidas By ECAL &quot;,
            &quot;year&quot;=&gt;&quot;2015&quot;,
            &quot;description&quot;=&gt;&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book&quot;,
            &quot;photos&quot;=&gt;[&quot;&quot;]
          },
        &quot;commit&quot;=&gt;&quot;Create Project&quot;,
        &quot;controller&quot;=&gt;&quot;projects&quot;,
        &quot;action&quot;=&gt;&quot;create&quot;
      }
  
      &gt;&gt; params[&quot;project&quot;][&quot;credit&quot;][&quot;creditable_type&quot;]
      =&gt; &quot;project&quot; 
       so I have that attribute but it's not inside the DB.
  </code></pre>
  <h2>8 - error message</h2>
  <pre><code>    =&gt; &quot;ERROR:  error param is missing or the value is empty: credit&quot;
    
  </code></pre>
  <h2>9 -  Param is missing cause the syntax is bad, and creditable_attributes are inside a hash, can't get the correct syntax to record them.</h2>
  <p>Hope that my question will help other junior dev to figure out the correct syntax to explore these nested form.</p>