请帮忙。安装Carrierwave花了两天时间,但是遇到了错误。显示/home/dima/rails_projects my_store/app/views/项目/show.html。未定义方法' image_url'为#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>
尝试了不同的变体(如不带'#')。请帮助学习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转换为string: INSERT INTO "items" ("created_at", "description", "image", "name", "price", "updated_at", "weight") VALUES (?, ?, ?, ?, ?)
假设has_one :image
是从Item到Image的引用,那么要引用图像路径,您应该使用:
<%= image_tag @item.image.image.url if @item.image %>
代替当前标签:<%= image_tag @item.image_url。to_s %> .
另外,你的表单写得好像mount_uploader:image是在Item模型上定义的,但是你的模型定义显示它首先必须通过image模型来获得载波图像。也许只是将图像直接挂载到项目上?