繁体   English   中英

Ruby on Rails-图片上传

[英]Ruby on Rails - Image Upload

我想将图像添加到模型中。 我有一些用Devise设置的用户,他们有他们要出售的have_many物品。

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

我看过回形针,但只能做一张图像。 还研究了载波,但不确定如何在现有模型上实现。

这是我的一些代码。

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

Item / 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>

您只想创建一个图像模型。 并设置与Item的关系,如下所示: Item has_many :images ,以及Image belongs_to :item

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

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

#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 您可以通过accepts_nested_attributes_for传递新images

#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 %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM