title: Ruby on Rails trouble creating record with has_many :through relationship
body: <p>I'm using rails 3.2.14 and having trouble using has_many :through for the first time. I'm using a has_many :through relationship between Image and Design tables using a Design_Pictures table that will store the order ranking for design images. All Images are associated with one User. Later I want the flexibility to store other images in the Images table that are not associated with a particular design. </p>
  
  <p>I can successfully add sample data to my database and show the image title (which is stored in the images table) and ranking (which is stored in the design_pictures table) in my show design pages. What I can't figure out is how to create a new design_picture. Once I can get this working I'm going to use CarrierWave or Paperclip to add images to the Image table.</p>
  
  <p>Here are my models:</p>
  
  <p><strong>app/models/image.rb:</strong></p>
  
  <pre><code>class Image &lt; ActiveRecord::Base
    attr_accessible :title
    belongs_to :user
    has_many :design_pictures, dependent: :destroy 
    has_many :designs, :through =&gt; :design_pictures
  
    validates :user_id, presence: true
    validates :title, length: { maximum: 80 }
  end
  </code></pre>
  
  <p><strong>app/models/design.rb:</strong></p>
  
  <pre><code>class Design &lt; ActiveRecord::Base
    attr_accessible :title
    has_many :design_pictures, dependent: :destroy 
    has_many :images, :through =&gt; :design_pictures
  
    validates :title, presence: true, length: { maximum: 60 }
  end
  </code></pre>
  
  <p><strong>app/models/design_picture.rb:</strong></p>
  
  <pre><code>class DesignPicture &lt; ActiveRecord::Base
    attr_accessible :ranking
    belongs_to :image
    belongs_to :design
  
    validates :image_id, presence: true
    validates :design_id, presence: true   
  
    default_scope order: 'design_pictures.ranking ASC'
  end
  </code></pre>
  
  <p><strong>app/models/user.rb:</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    attr_accessible :name
    has_many :designs, dependent: :destroy 
    has_many :images, dependent: :destroy
  
    validates :name, presence: true, length: { maximum: 50 }
  end
  </code></pre>
  
  <p>Views:</p>
  
  <p><strong>app/views/designs/show.html.erb:</strong></p>
  
  <pre><code>            &lt;div&gt;
                  &lt;% if @design.design_pictures.any? %&gt;
                      &lt;h3&gt;Images (&lt;%= @design.design_pictures.count %&gt;)&lt;/h3&gt;
                      &lt;ul&gt;
                          &lt;%= render @design_pictures %&gt;
                      &lt;/ul&gt;
                  &lt;% end %&gt;
              &lt;/div&gt;
  
              &lt;div&gt;
                  &lt;%= render 'shared/design_picture_form' %&gt;
              &lt;/div&gt;
  </code></pre>
  
  <p><strong>app/views/design_pictures/_design_picture.html.erb</strong></p>
  
  <pre><code>&lt;li&gt;
      &lt;%= design_picture.image.title %&gt; - Ranking: &lt;%= design_picture.ranking %&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p><strong>app/views/shared/_design_picture_form.html.erb:</strong></p>
  
  <pre><code>&lt;%= form_for(@design_picture) do |f| %&gt;
      &lt;%= render 'shared/error_messages', object: f.object %&gt;
  
      &lt;%= f.label :title, "Title" %&gt;
      &lt;%= f.text_field :title, 
                  placeholder: "Name your image.",
                  class: "" %&gt;
  
      &lt;%= f.label :ranking, "Ranking" %&gt;
      &lt;%= f.text_field :ranking, 
              placeholder: "Rank your design picture.",
              class: "" %&gt;            
  
      &lt;%= hidden_field_tag(:design_id, @design.id) %&gt;
  
      &lt;%= f.submit "Add Image", class: "btn btn-large btn-primary" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Controllers:</p>
  
  <p><strong>app/controllers/designs_controller.rb</strong></p>
  
  <pre><code>    def show
          @design = Design.find(params[:id])
          @design_pictures = @design.design_pictures.find(:all, :limit =&gt; 10)
  
          @image = current_user.images.build 
          @design_picture = @design.design_pictures.build
      end
  </code></pre>
  
  <p><strong>app/controllers/design_pictures_controller.rb</strong></p>
  
  <pre><code>    def create
          @current_design = Design.find(params[:design_id])
          @image = current_user.images.new(params[:title])
          @design_picture = @current_design.design_pictures.build(:image_id =&gt; @image.id, params[:ranking])
  
          if @design_picture.save
              flash[:success] = "Micropost created!"
              redirect_to @current_design
          else
              redirect_to designs_url
          end
      end
  </code></pre>
  
  <p>Error when I visit the design_picture form partial:</p>
  
  <pre><code>undefined method `title' for #&lt;DesignPicture image_id: nil, design_id: 1427, ranking: nil&gt;
  </code></pre>
  
  <p>If I remove the title from the form I can submit form but nothing is created. </p>
  
  <p>Any help would be greatly appreciated.</p>