【发布时间】:2017-06-01 14:13:31
【问题描述】:
我正在使用 Rails 创建一个数据库网站。我实现了用户可以上传图像的位置。有两个地方可以上传图片,一个用于供应商,另一个用于库存。其中一个模型称为“照片”,另一个模型称为“照片”。我是 Rails 新手,不知道如何使用相同的模型“照片”上传到数据库的其他部分,所以我创建了模型“照片”并按照教程进行操作。但是现在,当您上传照片时,按钮会显示“创建照片”。我想让它说“创建照片”。我不知道该怎么做。
我的一些代码文件
_form.html.erb/fotos/view
<%= form_for([@vendor, @vendor.fotos.build]) do |f| %>
<div class="form-group1">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control1'%>
</div>
<p>
<%= f.submit %>
</p>
<% end %>
_foto.html.erb/fotos/view
<div class="media1">
<div class="media-left1">
<%= link_to image_tag(foto.image.url, class: 'media-object1'), foto.image.url, target: '_blank' %>
</div>
<div class="media-body1">
<h4 class="media-heading1"><%= foto.title %></h4>
</div>
</div>
<p>
<%= link_to 'Delete Photo', [foto.vendor, foto],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
show.html.erb/vendors/view
<body>
<div class = "head">
<h1>Vendor Information</h1>
<div class = "image1" >
<img src= "http://dx.deucex.com/i/logo.png" >
</div>
</div>
</body>
<%= render @vendor.fotos %>
<h3>Add a photo:</h3>
<%= render 'fotos/form' %>
<p>
<strong>Company:</strong>
<%= @vendor.company %>
</p>
<p>
<strong>Contact Name:</strong>
<%= @vendor.contact_name %>
</p>
<p>
<strong>Phone:</strong>
<%= @vendor.phone %>
</p>
<p>
<strong>Email:</strong>
<%= @vendor.email %>
</p>
<p>
<strong>MOQ:</strong>
<%= @vendor.moq %>
</p>
<p>
<strong>Cost/Item:</strong>
<%= @vendor.cost_per_item %>
</p>
<p>
<strong>Payment Method:</strong>
<%= @vendor.payment_method %>
</p>
<p>
<strong>Terms:</strong>
<%= @vendor.terms %>
</p>
<p>
<strong>Turnover Period:</strong>
<%= @vendor.turnover %>
</p>
<p>
<strong>Returns:</strong>
<%= @vendor.returns %>
</p>
<p>
<strong>Notes:</strong>
<%= @vendor.notes %>
</p>
<%= link_to 'Back', vendors_path %>
fotos_controller.rb
class FotosController < ApplicationController
def index
@fotos = Foto.order('created_at')
end
def new
@foto = Foto.new
end
def create
@vendor = Vendor.find(params[:vendor_id])
@foto = @vendor.fotos.create(photo_params)
redirect_to vendor_path(@vendor)
end
def destroy
@vendor = Vendor.find(params[:vendor_id])
@foto = @vendor.fotos.find(params[:id])
@foto.destroy
redirect_to vendor_path(@vendor)
end
private
def photo_params
params.require(:foto).permit(:title, :image)
end
end
foto.rb/model
class Foto < ApplicationRecord
has_attached_file :image
belongs_to :vendor
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
迁移文件
class CreateFotos < ActiveRecord::Migration[5.1]
def change
create_table :fotos do |t|
t.string :title
t.references :vendor, foreign_key: true
t.timestamps
end
end
end
另一个迁移文件
class AddAttachmentImageToFotos < ActiveRecord::Migration[5.1]
def self.up
change_table :fotos do |t|
t.attachment :image
end
end
def self.down
remove_attachment :fotos, :image
end
end
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 cloud9-ide