У меня проблемы с скрепкой в моем приложении rails. Я могу прикрепить несколько файлов (PDF-файлов) к моей форме, но при попытке показать более 1 вложения в файле show.html.erb я получаю ошибки.
Код, который работает в режиме редактирования и новых видах:
<%= f.fields_for :assets do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :document %>
<% end %>
<% end %>
</div>
<div class="existingPaperclipFiles">
<% f.fields_for :assets do |asset| %>
<% unless asset.object.new_record? %>
<div class="thumbnail">
<%= link_to( image_tag(asset.object.document.url(:thumb)), asset.object.document.url(:original) ) %>
<%= asset.check_box :_destroy %>
</div>
<% end %>
<% end %>
</div>
Я создал отдельную модель активов, чтобы сохранить все вложения, связанные с моей моделью оборудования. Когда я создаю "link_to asset.object.document.url" в представлении представления, я получаю ошибки NoMethod. Я хочу прикрепить и .doc, PDF, и файлы изображений к моему приложению, если есть лучший способ, чем скрепка, пожалуйста, помогите!
Модель активов:
class Asset < ActiveRecord::Base
belongs_to :equipment
has_attached_file :document, :styles => {:thumb => '150x150#', :medium => '300x300#', :large => '600x600#' }
end
Модель оборудования:
class Equipment < ActiveRecord::Base
validates :equipment_id, presence: true
validates :location, presence: true
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
end
Контроллер моего оборудования:
class EquipmentController < ApplicationController
def index
@equipment = Equipment.paginate(page: params[:page])
end
def show
@equipment = Equipment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @equipment }
end
end
def new
@equipment = Equipment.new
5.times {@equipment.assets.build}
end
def create
@equipment = Equipment.new(params[:equipment])
respond_to do |format|
if @equipment.save
format.html { redirect_to @equipment, notice: 'Equipment was successfully added.' }
format.json { render json: @equipment, status: :created, location: @equipment }
else
format.html { render action: "new" }
format.json { render json: @equipment.errors, status: :unprocessable_entity }
end
end
end
def edit
@equipment = Equipment.find(params[:id])
5.times {@equipment.assets.build}
end
def update
@equipment = Equipment.find(params[:id])
respond_to do |format|
if @equipment.update_attributes(params[:equipment])
format.html { redirect_to @equipment, notice: 'Equipment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @equipment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@equipment = Equipment.find(params[:id])
@equipment.destroy
respond_to do |format|
format.html { redirect_to equipment_url }
format.json { head :no_content }
end
end
private
def correct_user
@Equipment = Equipment.find(params[:id])
redirect_to(root_path) unless current_user?(@Equipment)
end
def admin_user
redirect_to(root_path) unless current_user.admin?
end
end
Любая помощь очень ценится.