【发布时间】:2014-03-17 20:52:57
【问题描述】:
请帮忙。两天安装 Carrierwave,但因错误而停止: 显示 /home/dima/rails_projects/my_store/app/views/items/show.html.erb 其中第 8 行出现: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
图像.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>
尝试了不同的变体(比如没有'#')。请帮助学习 Rails。 (上次由截屏http://railscasts.com/episodes/253-carrierwave-file-uploads安装)
更新: 如果我使用
def item_params
params.require(:item).permit(:name, :description, :price, :weight, :real, :image)
end
有一个错误: TypeError:无法将 ActionDispatch::Http::UploadedFile 转换为字符串:INSERT INTO “items”(“created_at”、“description”、“image”、“name”、“price”、“updated_at”、“weight”)值(?,?,?,?,?,?,?)
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 carrierwave