Вот что происходит, когда я загружаю изображение в главный контроллер (контроллер A):
Вторичные сообщения, загружаемые из загруженного изображения основного сообщения
Чтобы объяснить изображение, первая картинка - это дочерняя запись в главной записи. Изображение внизу было первоначальной загрузкой при создании основного сообщения.
Я понимаю, почему это происходит ...
<p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide.image %></strike></p>
... но я не понимаю, как это изменить, чтобы любые последующие изображения, добавленные в главное сообщение, показывались специально для их сообщений вместо того же изображения, загруженного до создания сообщения. Страница, которая создает главный пост и позволяет загрузить изображение , показана здесь . Я попытался изменить код, указанный выше, чтобы он был следующим (подсказка: я добавил _item в @ purchase_guide.image) ...
<p style="opacity: 0.4;"><strike><%= buying_guide_item.content %>|<br><%= image_tag @buying_guide_item.image %></strike></p>
... но у меня вышла ошибка .
Контроллер 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
Под папкой buy_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 %>
Под папкой buy_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