I'm creating a database website using rails. I implemented where a user can upload an image. There are two places where you can upload an image, one for a vendor and other for the inventory. One of the model is called "photos" and the other "fotos". I'm new to rails and wasn't sure how to use the same model, "photos" to upload to the other part of the database, so I created the model "foto" and followed the tutorial. But now when you upload a photo, the button says "Create Foto". I want it to say "Create Photo". I'm not sure how to do this.
Some of my code files
_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>
http://ift.tt/2roJrRT
<body>
<div class = "head">
<h1>Vendor Information</h1>
<div class = "image1" >
<img src= "http://ift.tt/2r5JHqI" >
</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
migration file
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
another migration file
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
Aucun commentaire:
Enregistrer un commentaire