使用从属/辅助控制器上载图片

时间:2019-01-17 12:11:59

标签: ruby-on-rails

当我将图片上传到主控制器(控制器A)时会发生以下情况:

Secondary posts taking on primary post's uploaded image

为说明图片,第一张图片是主帖子的子帖子。底部的图像是创建主帖子时的初始上传。

我知道为什么会这样...

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

...但是我不知道如何更改它,因此添加到主帖子的任何后续图片都将专门针对其帖子显示,而不是创建帖子之前上传的同一图片。创建主帖子并允许我上传图像is shown here的页面。我尝试将上面引用的代码更改为以下代码(提示:我将_item添加到@ buying_guide.image)...

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

...但是我最终得到了error

控制器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

控制器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

模型A

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

B型

class BuyingGuideItem < ApplicationRecord
  belongs_to :buying_guide

  def completed?
    !completed_at.blank?
  end

  has_one_attached :image

end

在“ purchase_guide_items”视图文件夹下

_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 %>

在“ purchase_guides”视图文件夹下

_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>

迁移表-Buying_Guide_items引用了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 个答案:

没有答案