У меня есть полиморфная связь между моделями «Сервис» и «Изображение». Будет больше, чем одна модель, связанная с моделью изображения, но на данный момент существует только одна, поэтому именно поэтому модель «Image» является полиморфной. У меня также есть 4 разных типа изображений, поэтому я добавил 4 отношения has_many в модели «Сервис». И модель «Сервис» также добавила "accepts_nested_attributes_for :images"
.Несколько ошибок возврата полиморфных отношений has_many
Так что в _forms.html.erb у меня есть 2 разных fields_for
FormHelper, и каждый из них имеет разные источники. Но всякий раз, когда я пытаюсь создать новый, я получил ошибку "undefined method [] for nil:NilClass"
.
Так вот Сервисная и изображение модель:
class Service < ActiveRecord::Base
has_many :images, as: :imageable, :dependent => :destroy
has_many :images_title,-> {where "image_type = 2"}, as: :imageable, class_name: "Image", :dependent => :destroy
has_many :images_icon,-> {where "image_type = 1"}, as: :imageable, class_name: "Image", :dependent => :destroy
has_many :images_regular,-> {where "image_type = 3"}, as: :imageable, class_name: "Image", :dependent => :destroy
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
mount_uploader :image, ImageUploader
enum image_type: {icon: 1, title: 2, regular: 3, slider: 4}
end
ServiceController:
def new
@service = Service.new
@images = @service.images.build
end
def create
@service = Service.new(service_params)
if @service.save
@image_title = @service.images.create!(:image => params[:images_title][:image], :imageable_id => @service.id, :image_type => params[:images_title][:image_type])
@icon_image = @service.images.create!(:image => params[:images_icon][:image], :imageable_id => @service.id, :image_type => params[:images_icon][:image_type])
# if params[:images_icon]
# end
flash[:notice] = "Hizmet sayfası başarıyla oluşturuldu!"
redirect_to admin_services_path
else
flash[:alert] = "Hizmet sayfası eklenirken bir hata oluştu. Lütfen tekrar deneyiniz!"
render "new"
end
end
private
def service_params
params.require(:service).permit(:service_name, :service_description, :meta_keywords,
images_title_attributes: [:id, :imageable_id, :image], images_icon_attributes: [:id, :imageable_id, :image])
end
_form.html.erb:
<div class="control-group">
<%= f.simple_fields_for :images_title do |image| %>
<div class="controls">
<label class="control-label">Hizmet Büyük Resimi</label>
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
<% if image.object.nil? %>
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" alt=""/>
<% else %>
<%= image_tag image.object.image_url %>
<% end %>
</div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Resim Seç</span><span class="fileupload-exists">Değiştir</span>
<%= image.input :image, input_html: { type: :file, class: "default"}, wrapper: false, label: false %>
<%= image.input :image_type, :as => "hidden", :input_html => { :value => :title } %>
</span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Sil</a>
</div>
</div>
</div>
<% end %>
</div>
Сервис вставляется правильно, но в вложенном У меня есть ошибка. Так что я сделал что-то не так?