Question

Please help. Two days installing Carrierwave, but stopped at errors: Showing /home/dima/rails_projects/my_store/app/views/items/show.html.erb where line #8 raised: undefined method `image_url' for # items_controller.rb:

 def show
    unless @item 
      render text: "Page not here", status: 404
    end
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.create(item_params)
    if @item.errors.empty?
      redirect_to item_path(@item)
    else
      render "new"
    end
  end

  def update
    @item.update_attributes(item_params)
    if @item.errors.empty?
      redirect_to item_path(@item)
    else
      render "edit"
    end
  end

    def item_params
      params.require(:item).permit(:name, :description, :price, :weight, :real, :image_attributes)
    end
end

show.html.erb:

<h1><%= @item.name%></h1>
<ul>
    <li>Цена: <%= @item.price %> грн.</li>
   <li>Описание: <%= urls_to_links(urls_to_images(@item.description)) %></li>
   <li>Вес: <%= @item.weight %> кг</li>
   <li>Реальный: <%= @item.real %></li>
   <%= image_tag @item.image_url.to_s %>
 </ul>

item.rb

class Item < ActiveRecord::Base

  validates :price, numericality: { greater_than: 0, allow_nil: true } 
  validates :name, :description, presence: true

  #  has_one :image
  # accepts_nested_attributes_for :image
end

image.rb

class Image < ActiveRecord::Base
  mount_uploader :image, ImageUploader
  # belongs_to :imageable, polymorphic: true
end

new.html.erb

<h1>Create new item</h1>
<%= form_for @item, :html => {:multipart => true} do |f| %>
  <%= render partial: "form", locals: { f: f } %>
  <p><%= f.submit "Создать товар" %></p>
<% end %>

_form.html.erb

<p>Наименоваие: <%= f.text_field :name %></p>
<p>Описание: <%= f.text_field :description %></p>
<p>Цена: <%= f.text_field :price %></p>
<p>Вес: <%= f.text_field :weight %></p>
<p>Реальный: <%= f.text_field :real %></p>
<label>My Image</label>
<p><%= f.file_field :image %></p>

Tried different variants (like withouts '#'). Please help in leaning Rails. (Last time installed by screencast http://railscasts.com/episodes/253-carrierwave-file-uploads )

Upd: If I use

    def item_params
      params.require(:item).permit(:name, :description, :price, :weight, :real, :image)
    end

Have an error: TypeError: can't cast ActionDispatch::Http::UploadedFile to string: INSERT INTO "items" ("created_at", "description", "image", "name", "price", "updated_at", "weight") VALUES (?, ?, ?, ?, ?, ?, ?)

Was it helpful?

Solution

Assuming that has_one :image is the reference from Item to Image, then to reference the image path you should use:

<%= image_tag @item.image.image.url if @item.image %>

instead of your current tag: <%= image_tag @item.image_url.to_s %>.

Additionally your form is written as if mount_uploader :image is defined on the Item model, but your model definition shows that it first has to go through the Image model to get the carrierwave image. Perhaps just mount the image directly on item?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top