Rails 5 CarrierWave在嵌套表单中上传多态图像会抛出NoMethod错误"未定义方法`image_changed?'"

时间:2016-10-29 13:10:59

标签: ruby-on-rails ruby carrierwave nested-forms polymorphic-associations

我想通过CarrierWave上传一个多态图像,但我似乎无法正确获取所有细节。目前,我在标题中得到NoM​​ethod错误,"未定义的方法`image_changed?'"我发现已经发布了许多相关问题,但提供的解决方案并不适用于我的案例,至少我能够找到。任何帮助表示赞赏。这是相关的代码。如果我能提供其他任何信息,请告诉我。

者:

# app/uploaders/image_uploader.rb
`class ImageUploader < CarrierWave::Uploader::Base
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fill: [280, 280]
  end

  version :small_thumb, :from_version => :thumb do
    process resize_to_fill: [20, 20]
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

图片模型:

#app/models/image.rb
class Image < ApplicationRecord
  mount_uploader :image, ImageUploader
  belongs_to :imageable, polymorphic: true
end

发布模型:

#app/models/post.rb
class Post < ApplicationRecord
  mount_uploader :image, ImageUploader
  has_one :image, as: :imageable, dependent: :destroy

  validates :title, presence: true, length: { maximum: 150, minumum: 3 }
  validates :content, presence: true, length: { minimum: 5 }

  scope :featured, -> { where(is_featured: true) }
  scope :authored_by, -> (user) { where(:user => user) }
end

后控制器:

#app/controllers/admin/posts_controller
class Admin::PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  def update
    unless params[:post][:image].empty?
      @post.build_image(params[:post][:image][:original_filename]).save
    end

    respond_to do |format|
      if @post.update_attributes(post_params)
        format.html { redirect_to permalink_path(:blog => @blog.name.parameterize, :date => @post.link_date, :title => @post.title.parameterize) }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.require(:post).permit(:title, :content, :user_id, :weblog_id, :is_featured, :category_ids => [], :tag_ids => [],
        image_attributes: [:image])
    end
end

编辑表格:

#app/views/admin/posts/_form.html.erb
<div class="container">
<%= form_for [:admin,post], :html => {:multipart => true} do |f| %>
  <div class="row">
    <div class="form-group">
      <%= f.label :title %><br/>
      <%= f.text_field :title, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.label :content %><br/>
      <%= f.text_area :content, class: "form-control ckeditor" %>
    </div>

    <div class="form-group">
      <%= f.check_box :is_featured %>
      <%= f.label :is_featured %>?
    </div>

    <h4>Image</h4>
    <%= f.fields_for Image.new do |i| %>
      <div class="row">
        <div class="col-md-4">
          <%= image_tag(post.image_url, class: "img-responsive")  if post.image? %>
        </div>
        <div class="col-md-8">
          <div class="form-group">
            <%= i.label :image, "Choose Image" %><br/>
            <%= i.file_field :image %>
            <%= f.hidden_field :image_cache %>
          </div>
        </div>
      </div>
    <% end %>

    <div class="form-group margin-top30">
      <%= f.submit class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

0 个答案:

没有答案