Search code examples
ruby-on-railsundefinedcarrierwaveimageurlcarrier

CarrierWave undefined method `image_url' or broken image


I have many images(add_images_to_steps) belonging to steps which belongs to diys. All information saves correctly as DB browser shows, but i have problem viewing images.

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

I get NoMethodError in Diys#Show

undefined method `image_url' for #

and if I change

to

I get this

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

Heres My migrations, models and controllers.

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

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

models/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

models/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

models/add_images_to_step.rb

 class AddImagesToStep < ActiveRecord::Base
   belongs_to :step
 end

Diy migration

 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

Steps migration

 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 migration

 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

Solution

  • I figured it out!!

    Had to add

    mount_uploader :image, ImageUploader

    to ../models/add_images_to_step.rb

    change def store_dir to

    "#{Rails.root}/public/uploads/"

    in ../app/uploaders/image_uploader.rb

    and

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

    was right for show view.