Ruby on Rails - 图片上传

时间:2016-01-21 20:30:28

标签: ruby-on-rails

我想将图像添加到模型中。我让用户设置了Devise,他们正在销售have_many项目。

我现在想在Item模型中添加一个图像数组(不确定这是否是最好的方法)。

我看了一个Paperclip,但只能做一张图片。还看了载波但不确定如何在现有模型上实现。

以下是我的一些代码。

item.rb的

class Item < ActiveRecord::Base
  belongs_to :user

  validates :title, presence: true
  validates :price, presence: true
  validates :description, presence: true
end

items_controller.rb

class ItemsController < ApplicationController
  before_action :findItem, only: [:edit, :update, :sold]

  def index
    @items = Item.all
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.create(item_params)
    @item.user = current_user
    if @item.save
      flash[:success] = "Your item was successfully listed."
      render 'show'
    else
      flash[:error] = "Your item could not be listed. Please try again."
      render 'new'
    end
  end

  def show
    @item = Item.find(params[:id])
  end

  def edit
  end

  def update
    if @item.update(item_params)
      flash[:success] = "Your item listing was updated successfully."
      redirect_to item_path(@item)
    else
      flash[:error] = "Your listing was not updated. Please try again."
      render 'edit'
    end
  end

  def sold
    @item.toggle(:sold)
    @item.save
    redirect_to item_path(@item)
  end

  private
  def item_params
    params.require(:item).permit(:title, :price, :description)
  end

  def findItem
    @item = Item.find(params[:id])
  end
end

表单/ new.html.erb

<div class="row">
  <div class="col-xs-12">
    <%= form_for(@item, :html => {class: "form-horizontal", role: "form"}) do |f| %>
      <div class="form-group">
    <div class="control-label col-sm-2">
      <%= f.label :title %>
    </div>
    <div class="col-sm-8">
      <%= f.text_field :title, class: "form-control", placeholder: "What are you selling?", autofocus: true %>
    </div>
  </div>

  <div class="form-group">
    <div class="control-label col-sm-2">
      <%= f.label :price %>
    </div>
    <div class="col-sm-8">
      <%= f.number_field :price, class: "form-control" %>
    </div>
  </div>

  <div class="form-group">
    <div class="control-label col-sm-2">
      <%= f.label :description %>
    </div>
    <div class="col-sm-8">
      <%= f.text_area :description, rows: 10, class: "form-control", placeholder: "Describe your item. The more detail you include, the more likely it is to sell." %>
    </div>
  </div>

  <div class="form-group">
    <div class="center col-sm-offset-1 col-sm-10">
      <%= f.submit class: "btn btn-primary btn-lg" %>
    </div>
  </div>
<% end %>

<div class="center col-xs-4 col-xs-offset-4">
  <%= link_to "[ Cancel and return to listing page ]", items_path %>
</div>

2 个答案:

答案 0 :(得分:2)

您只想创建一个Image模型。并按如下方式设置与Item的关系:Item has_many :imagesImage belongs_to :item

但是,是的,回形针是一个好的开始。

编辑:哦,欢迎Rails,你会发现一个好的社区随时准备提供帮助。您可能还会发现搜索accepts_nested_attributes_for非常有用,因此您可以在项目表单中上传图片,cocoon可以在项目表单上动态添加和删除图片。

答案 1 :(得分:0)

#app/models/item.rb
class Item < ActiveRecord::Base
   has_many :images
   accepts_nested_attributes_for :images
   validates :title, :price, :description, presence: true
end

#app/models/image.rb
class Image < ActiveRecord::Base
   belongs_to :item
   has_attached_file :picture
end

-

以上内容允许您创建Item,其中包含您想要的任意数量的images。您可以将新的images传递给accepts_nested_attributes_for

#app/controllers/items_controller.rb
class ItemsController < ApplicationController
   def new
      @item = Item.new
      @item.images.build
   end

   def create
      @item = Item.new item_params
      @item.save
   end

   private

   def item_params
      params.require(:item).permit(images_attributes: [:picture])
   end
end

#app/views/items/new.html.erb
<%= form_for @item do |f| %>
   <%= f.fields_for :images do |i| %>
      <%= i.file_field :picture %>
   <% end %>
   <%= f.submit %>
<% end %>