title: Uploading pictures using the dependent/secondary controller
body: <p>This is what happens when I upload a picture to the main controller (controller A):</p>
  
  <p><a href="https://i.stack.imgur.com/jMT3z.jpg" rel="nofollow noreferrer">Secondary posts taking on primary post's uploaded image</a></p>
  
  <p>To explain the image, the first pic is a child post to the master post. The image at the bottom was the initial upload upon creation of the master post.</p>
  
  <p>I understand why this is happening...</p>
  
  <pre><code>&lt;p style="opacity: 0.4;"&gt;&lt;strike&gt;&lt;%= buying_guide_item.content %&gt;|&lt;br&gt;&lt;%= image_tag @buying_guide.image %&gt;&lt;/strike&gt;&lt;/p&gt;
  </code></pre>
  
  <p>...but I don't understand how to change it so that any subsequent pictures added to the master post are showing specifically for their posts instead of the same image uploaded before the post was created. The page that creates the master post and allows me to upload an image <a href="https://i.stack.imgur.com/xf7wP.png" rel="nofollow noreferrer">is shown here</a>. I tried changing the code quoted above to be the following code (hint: I added _item to @buying_guide.image)...</p>
  
  <pre><code>&lt;p style="opacity: 0.4;"&gt;&lt;strike&gt;&lt;%= buying_guide_item.content %&gt;|&lt;br&gt;&lt;%= image_tag @buying_guide_item.image %&gt;&lt;/strike&gt;&lt;/p&gt;
  </code></pre>
  
  <p>...but I ended up with an <a href="https://i.stack.imgur.com/NVusH.png" rel="nofollow noreferrer">error</a>.</p>
  
  <p>Controller A</p>
  
  <pre><code>class BuyingGuidesController &lt; ApplicationController
    before_action :set_buying_guide, only: [:show, :edit, :update, :destroy]
  
    # GET /buying_guides
    # GET /buying_guides.json
    def index
      @buying_guides = BuyingGuide.all
    end
  
    # GET /buying_guides/1
    # GET /buying_guides/1.json
    def show
    end
  
    # GET /buying_guides/new
    def new
      @buying_guide = BuyingGuide.new
    end
  
    # GET /buying_guides/1/edit
    def edit
    end
  
    # POST /buying_guides
    # POST /buying_guides.json
    def create
      @buying_guide = BuyingGuide.new(buying_guide_params)
  
      respond_to do |format|
        if @buying_guide.save
          format.html { redirect_to @buying_guide, notice: 'Buying guide was successfully created.' }
          format.json { render :show, status: :created, location: @buying_guide }
        else
          format.html { render :new }
          format.json { render json: @buying_guide.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /buying_guides/1
    # PATCH/PUT /buying_guides/1.json
    def update
      respond_to do |format|
        if @buying_guide.update(buying_guide_params)
          format.html { redirect_to @buying_guide, notice: 'Buying guide was successfully updated.' }
          format.json { render :show, status: :ok, location: @buying_guide }
        else
          format.html { render :edit }
          format.json { render json: @buying_guide.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /buying_guides/1
    # DELETE /buying_guides/1.json
    def destroy
      @buying_guide.destroy
      respond_to do |format|
        format.html { redirect_to buying_guides_url, notice: 'Buying guide was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_buying_guide
        @buying_guide = BuyingGuide.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def buying_guide_params
        params.require(:buying_guide).permit(:title, :description, :image)
      end
  
  end
  </code></pre>
  
  <p>Controller B</p>
  
  <pre><code>class BuyingGuideItemsController &lt; ApplicationController
  
      before_action :set_buying_guide
      before_action :set_buying_guide_item, except: [:create]
  
      def create
          @buying_guide_item = @buying_guide.buying_guide_items.create(buying_guide_item_params)
          redirect_to @buying_guide
      end
  
      def destroy
          @buying_guide_item = @buying_guide.buying_guide_items.find(params[:id])
          if @buying_guide_item.destroy
              flash[:success] = "Buying guide list item was deleted."
          else
              flash[:error] = "Buying guide list item could not be deleted."
          end
          redirect_to @buying_guide
      end
  
      def complete
          @buying_guide_item.update_attribute(:completed_at, Time.now)
          redirect_to @buying_guide, notice: "Buying guide item completed."
      end
  
      private
  
      def set_buying_guide
          @buying_guide = BuyingGuide.find(params[:buying_guide_id])
      end
  
      def set_buying_guide_item
          @buying_guide_item = @buying_guide.buying_guide_items.find(params[:id])
      end
  
      def buying_guide_item_params
          params[:buying_guide_item].permit(:content, :image)
      end
  
  end
  </code></pre>
  
  <p>Model A</p>
  
  <pre><code>class BuyingGuide &lt; ApplicationRecord
      has_many :buying_guide_items, :dependent =&gt; :destroy
      has_one_attached :image
  end
  </code></pre>
  
  <p>Model B</p>
  
  <pre><code>class BuyingGuideItem &lt; ApplicationRecord
    belongs_to :buying_guide
  
    def completed?
      !completed_at.blank?
    end
  
    has_one_attached :image
  
  end
  </code></pre>
  
  <p><strong>Under buying_guide_items view folder</strong></p>
  
  <p>_buying_guide_item.html.erb</p>
  
  <pre><code>&lt;div class="row clearfix"&gt;
      &lt;% if buying_guide_item.completed? %&gt;
  
      &lt;div class="complete"&gt;
      &lt;%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
      :patch do %&gt;
          &lt;i style="opacity: 0.4;" class="fa fa-check"&gt;&lt;/i&gt;
              &lt;% end %&gt;
      &lt;/div&gt;
  
      &lt;div class="item"&gt;
      &lt;p style="opacity: 0.4;"&gt;&lt;strike&gt;&lt;%= buying_guide_item.content %&gt;|&lt;br&gt;&lt;%= image_tag @buying_guide.image %&gt;&lt;/strike&gt;&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="trash"&gt;
      &lt;%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
      method: :delete, data: { confirm: "Are you sure?" } do %&gt;
          &lt;i class="fa fa-trash"&gt;&lt;/i&gt;
              &lt;% end %&gt;
      &lt;/div&gt;
  
      &lt;% else %&gt;
  
      &lt;div class="complete"&gt;
      &lt;%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
      :patch do %&gt;
          &lt;i class="fa fa-circle-thin"&gt;&lt;/i&gt;
              &lt;% end %&gt;
      &lt;/div&gt;
  
      &lt;div class="item"&gt;
      &lt;p&gt;&lt;%= buying_guide_item.content %&gt;|&lt;br&gt;&lt;%= image_tag @buying_guide.image %&gt;&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="trash"&gt;
      &lt;%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
      method: :delete, data: { confirm: "Are you sure?" } do %&gt;
          &lt;i class="fa fa-trash"&gt;&lt;/i&gt;
              &lt;% end %&gt;
      &lt;/div&gt;
  
      &lt;% end %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_for([@buying_guide, @buying_guide.buying_guide_items.build]) do |f| %&gt;
  &lt;br&gt;
  &lt;%= f.text_field :content, placeholder: "..." %&gt;
  &lt;%= f.file_field :image %&gt;
  &lt;%= f.submit :"Submit" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Under buying_guides view folder</strong></p>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_with(model: buying_guide, local: true) do |form| %&gt;
    &lt;% if buying_guide.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(buying_guide.errors.count, "error") %&gt; prohibited this buying_guide from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% buying_guide.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= form.label :title %&gt;
      &lt;%= form.text_field :title %&gt;
    &lt;/div&gt;
  
    &lt;div class="field"&gt;
      &lt;%= form.label :description %&gt;
      &lt;%= form.text_area :description %&gt;
    &lt;/div&gt;
  
    &lt;div&gt;
    &lt;%= form.label :image %&gt;
    &lt;%= form.file_field :image %&gt;
    &lt;/div&gt;
  
    &lt;div&gt;
      &lt;%= form.submit :"Submit" %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>show.html.erb</p>
  
  <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt;
  
  &lt;h2 class="list_title"&gt;&lt;%= @buying_guide.title %&gt;&lt;/h2&gt;
  
  &lt;div id="items_wrapper"&gt;
    &lt;%= render @buying_guide.buying_guide_items %&gt;
    &lt;%= image_tag @buying_guide.image %&gt;
      &lt;div id="form"&gt;
        &lt;%= render "buying_guide_items/form" %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="links"&gt;
  &lt;%= link_to 'Back', buying_guides_path %&gt; |
  &lt;%= link_to 'Edit', edit_buying_guide_path(@buying_guide) %&gt; |
  &lt;%= link_to 'Delete', buying_guide_path(@buying_guide), method: :delete, data:
  { confirm: "Are you sure?" } %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Migration table</strong> - Buying_Guide_items is referencing Buying_Guide</p>
  
  <pre><code>class CreateBuyingGuideItems &lt; ActiveRecord::Migration[5.2]
  
    def change
      create_table :buying_guide_items do |t|
        t.string :content
        t.references :buying_guide, foreign_key: true
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>Routes.rb</p>
  
  <pre><code>Rails.application.routes.draw do
      get 'welcome/index'
  
      resources :buying_guides do
        resources :buying_guide_items do
          member do
            patch :complete
          end
        end
      end
  </code></pre>