用于宝石载波多张图像上传的导轨

时间:2021-01-28 07:15:47

标签: ruby-on-rails ruby

导轨 5.2.4.4 红宝石 2.5.1p57 sqlite3

我想做什么:使用载波和多张图像上传。

问题:nil:NilClass 的未定义方法 `url'。

谁能帮帮我。

       new.html.erb
       <%= form_for [:admins,@item], local: true do |f| %>
        <%= render 'shared/errors', object: f.object%>

        <%= f.label :name %>
        <%= f.text_field :name, class: 'form-control mb-3'%>

        <%= f.label :price %>
        <%= f.text_field :price, class: 'form-control mb-3'%>
        
        <%= f.file_field :imgs, multiple: true %>

        <%= f.label :Video_url %>
        <%= f.text_field :Video_url, class: 'form-control' %>

        <%= f.label :discription %>
        <%= f.text_area :discription, class: 'form-control' %>


        <%= f.submit "Create my item", class: "btn btn-primary d-block" %>
      <% end %>
index.html.erb
<% @items.each do |item| %>

  Problem:  undefined method `url' for nil:NilClass.
  1..<%= link_to(image_tag(item.imgs[0].url || "19.jpg"), item, class: "img-responsive") %>

  This is good but it is too big.
  2..<%= link_to(image_tag(item.imgs[0].to_s || "19.jpg"), item, class: "img-responsive") %>

  It doesn't work why?
  undefined method `thumb' for nil:NilClass
  3..<%= link_to(image_tag(item.imgs[0].thumb.to_s || "19.jpg"), item, class: "img-responsive") %>
        
<% end %>

以上这些代码,谁能告诉我为什么第三个代码不起作用,什么是不同的 url 和 to_s 和 reason 可以工作吗?

controller
  def new
    @item = current_admin.items.build
  end

  def create
    @item = current_admin.items.build(item_params)
    if @item.save
      flash[:success] = "Your item is posted"
      redirect_to root_path
    else
      render 'new'
    end

  end

  private
  def item_params
    params.require(:item).permit(:name, :price, :discription, :Video_url,{imgs: []})
  end
image_uploader
class ApplicationUploader < CarrierWave::Uploader::Base
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

class ImageUploader < ApplicationUploader
  process resize_to_fit: [800, 800]

  # item/index
  version :thumb do
    process resize_to_fill: [300,400]
  end

  # basket/show
  version :thumb50 do
    process resize_to_fill: [120,150]
  end
end
(my data is sqlite3)
  item.rb

  mount_uploaders :imgs, ImageUploader
  serialize :imgs, JSON
my rails c

[1] pry(main)> item1 = Item.first

[6] pry(main)> item1.imgs[0].url
=> "/uploads/item/imgs/71/su1.jpg"

pry(main)> item1.imgs[1].url
=> "/uploads/item/imgs/71/top.jpg"

0 个答案:

没有答案
相关问题