Неопределенный метод CarrierWave `image_url'или поврежденное изображение

У меня есть много изображений (add_images_to_steps), принадлежащих шагам, которые принадлежат diys. Вся информация сохраняется правильно, как показывает браузер БД, но у меня проблемы с просмотром изображений.

сviews/diys/show.html.erb

    <p id="notice"><%= notice %></p>
    <h2><%= @diy.summary %></h2>
      <% @steps.each do |step| %>
        <p><%= step.step_content %></p>
        <% step.add_images_to_steps.each do |i| %>
          <%= image_tag i.image_url.to_s %>
        <% end %>
    <% end %>

Я получаю NoMethodError в Diys#Show

неопределенный метод `image_url'для #

и если я изменюсь

в

Я понял

-------------------------------------------------- ---------------------

Вот мои миграции, модели и контроллеры.

-------------------------------------------------- ---------------------

diys_controller.rb

 class DiysController < ApplicationController
   before_action :set_diy, only: [:show, :edit, :update, :destroy]

 def show
   @diy = Diy.find(params[:id])
   @steps = @diy.steps.all
   @diy.add_images_to_steps.all
 end

 def new
   @diy = Diy.new
   @step = @diy.steps.new
   @step.add_images_to_steps.new
 end

 ...

 def diy_params
   params.require(:diy).permit(:title, :summary, :tip, :warning, steps_attributes: [:step_content, add_images_to_steps_attributes: [:image]])
 end

модели /diy.rb

class Diy < ActiveRecord::Base
  has_many :steps, dependent: :destroy
  accepts_nested_attributes_for :steps, reject_if: :all_blank
  has_many :add_images_to_steps, :through => :steps, dependent: :destroy
  accepts_nested_attributes_for :add_images_to_steps
end

модели /step.rb

 class Step < ActiveRecord::Base
   belongs_to :diy
   has_many :add_images_to_steps, dependent: :destroy
   accepts_nested_attributes_for :add_images_to_steps
   mount_uploader :image, ImageUploader
 end

модели /add_images_to_step.rb

 class AddImagesToStep < ActiveRecord::Base
   belongs_to :step
 end

Сделай сам миграция

 class CreateDiys < ActiveRecord::Migration
   def change
     create_table :diys do |t|
       t.string :title
       t.text :summary
       t.text :tip
       t.text :warning
       t.timestamps null: false
     end
   end
 end

Шаги миграции

 class CreateSteps < ActiveRecord::Migration
   def change
     create_table :steps do |t|
       t.belongs_to :diy
       t.text :step_content
       t.timestamps null: false
     end
   end
 end

миграция add_images_to_step

 class CreateAddImagesToSteps < ActiveRecord::Migration
   def change
     create_table :add_images_to_steps do |t|
       t.belongs_to :step
       t.string :image
       t.timestamps null: false
     end
   end
 end

1 ответ

Решение

Я понял!!

Пришлось добавить

mount_uploader: изображение, ImageUploader

в../models/add_images_to_step.rb

измените def store_dir на

"#{Rails.root}/ государственные / загрузки /"

в../app/uploaders/image_uploader.rb

а также

"<% = image_tag i.image_url.to_s%>"

был прав для показа.

Другие вопросы по тегам