多个has_many多态关系返回错误

时间:2015-05-05 15:33:26

标签: ruby-on-rails ruby-on-rails-4 activerecord

我在"服务"之间有多态关系。和"图像"楷模。将有不止一个与Image模型相关的模型,但现在只有一个,所以这就是为什么" Image"模型是多态的。我还有4种不同的图像类型,所以我在"服务"中添加了4个has_many关系。模型。并且"服务"模型还添加了"accepts_nested_attributes_for :images"

所以在_forms.html.erb中,我有两个不同的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&amp;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>

正在正确插入服务但在嵌套属性中我收到了错误。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我经常使用多态关系,而且我已经完成了你想要做的事情:http://6ftdan.com/allyourdev/2014/08/01/manual-polymorphic-creation-in-rails/你正在实例化多种“类型”的图像。我在博客中展示了如何使用枚举,与 image_type 字段相同。

博客文章涵盖了从控制器到表单的所有内容。在我的示例中,我将四种不同类型的相同记录实例化为表单中的嵌套属性。这样就可以为你完成工作。

简单地说,您将要更改的是,您的图片只需要一个 has_many 行。另一件重要的事情是你需要对将通过表单上传的每个图像实例使用构建