Uploading pictures using the dependent/secondary controller

30 views Asked by At

This is what happens when I upload a picture to the main controller (controller A):

Secondary posts taking on primary post's uploaded image

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.

I understand why this is happening...

<p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></strike></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 is shown here. I tried changing the code quoted above to be the following code (hint: I added _item to @buying_guide.image)...

<p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide_item.image %></strike></p>

...but I ended up with an error.

Controller A

class BuyingGuidesController < 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

Controller B

class BuyingGuideItemsController < 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

Model A

class BuyingGuide < ApplicationRecord
    has_many :buying_guide_items, :dependent => :destroy
    has_one_attached :image
end

Model B

class BuyingGuideItem < ApplicationRecord
  belongs_to :buying_guide

  def completed?
    !completed_at.blank?
  end

  has_one_attached :image

end

Under buying_guide_items view folder

_buying_guide_item.html.erb

<div class="row clearfix">
    <% if buying_guide_item.completed? %>

    <div class="complete">
    <%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
    :patch do %>
        <i style="opacity: 0.4;" class="fa fa-check"></i>
            <% end %>
    </div>

    <div class="item">
    <p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></strike></p>
    </div>

    <div class="trash">
    <%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
    method: :delete, data: { confirm: "Are you sure?" } do %>
        <i class="fa fa-trash"></i>
            <% end %>
    </div>

    <% else %>

    <div class="complete">
    <%= link_to complete_buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id), method:
    :patch do %>
        <i class="fa fa-circle-thin"></i>
            <% end %>
    </div>

    <div class="item">
    <p><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></p>
    </div>
    <div class="trash">
    <%= link_to buying_guide_buying_guide_item_path(@buying_guide, buying_guide_item.id),
    method: :delete, data: { confirm: "Are you sure?" } do %>
        <i class="fa fa-trash"></i>
            <% end %>
    </div>

    <% end %>
</div>

_form.html.erb

<%= form_for([@buying_guide, @buying_guide.buying_guide_items.build]) do |f| %>
<br>
<%= f.text_field :content, placeholder: "..." %>
<%= f.file_field :image %>
<%= f.submit :"Submit" %>
<% end %>

Under buying_guides view folder

_form.html.erb

<%= form_with(model: buying_guide, local: true) do |form| %>
  <% if buying_guide.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(buying_guide.errors.count, "error") %> prohibited this buying_guide from being saved:</h2>

      <ul>
      <% buying_guide.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :description %>
    <%= form.text_area :description %>
  </div>

  <div>
  <%= form.label :image %>
  <%= form.file_field :image %>
  </div>

  <div>
    <%= form.submit :"Submit" %>
  </div>
<% end %>

show.html.erb

<p id="notice"><%= notice %></p>

<h2 class="list_title"><%= @buying_guide.title %></h2>

<div id="items_wrapper">
  <%= render @buying_guide.buying_guide_items %>
  <%= image_tag @buying_guide.image %>
    <div id="form">
      <%= render "buying_guide_items/form" %>
    </div>
</div>

<div class="links">
<%= link_to 'Back', buying_guides_path %> |
<%= link_to 'Edit', edit_buying_guide_path(@buying_guide) %> |
<%= link_to 'Delete', buying_guide_path(@buying_guide), method: :delete, data:
{ confirm: "Are you sure?" } %>
</div>

Migration table - Buying_Guide_items is referencing Buying_Guide

class CreateBuyingGuideItems < 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

Routes.rb

Rails.application.routes.draw do
    get 'welcome/index'

    resources :buying_guides do
      resources :buying_guide_items do
        member do
          patch :complete
        end
      end
    end
0

There are 0 answers