2016-01-28 73 views
0

我有許多圖像(add_images_to_steps)屬於屬於diys的步驟。所有信息都以DB瀏覽器顯示正確保存,但我在查看圖像時遇到問題。CarrierWave未定義的方法`image_url'或破碎的圖像

的意見/小五金/ 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 %> 

我得到NoM​​ethodError中小五金#顯示

未定義的方法`IMAGE_URL」爲#

如果我改變

I get this

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

繼承人我的遷移,模型和控制器。

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

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 
+0

嘗試image_url image_url intead – Ezequiel

+0

不起作用:(但謝謝你反正 –

回答

0

我想通了!

不得不添加

mount_uploader:圖像,ImageUploader

到../models/add_images_to_step.rb

變化高清store_dir到

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

在../app/uploaders/image_uploader.rb

「<%= IMAGE_TAG i.image_url.to_s%>」

是對用於放映視圖

相關問題