回形针:“缺失”图像

时间:2010-12-21 22:51:28

标签: ruby-on-rails ruby-on-rails-plugins paperclip ruby-on-rails-3

我正在建立一个网站,允许经营床位和早餐的人发布他们的住宿。

我想要求他们在发布时包含住宿的“个人资料图片”,但我也想让他们选择稍后添加更多图片(这将在之后开发)。

我认为最好的办法是使用Paperclip宝石,并在我的应用程序中有一个住宿和照片,后者属于第一个作为协会。

创建住宿时会创建新的照片记录。它具有id和accommodation_id属性。但是,图像永远不会上传,并且没有设置Paperclip属性(image_file_name:nil,image_content_type:nil,image_file_size:nil),因此我得到了Paperclip的“缺失”照片。

关于这个的任何想法?它一直让我陷入困境几天。

住宿

模型/ accommodation.rb

class Accommodation < ActiveRecord::Base
  validates_presence_of :title, :description, :photo, :thing, :location
  attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
  has_one :photo
end

控制器/ accommodation_controller.erb

class AccommodationsController < ApplicationController

  before_filter :login_required, :only => {:new, :edit}

  uses_tiny_mce ( :options => {
    :theme => 'advanced',
    :theme_advanced_toolbar_location => 'top',
    :theme_advanced_toolbar_align => 'left',
    :theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
    :theme_advanced_buttons2 => '',
    :theme_advanced_buttons3 => ''
  })

  def index
    @accommodations = Accommodation.all
  end

  def show
    @accommodation = Accommodation.find(params[:id])
  end

  def new
    @accommodation = Accommodation.new
  end

  def create
    @accommodation = Accommodation.new(params[:accommodation])
    @accommodation.photo = Photo.new(params[:photo])
    @accommodation.user_id = current_user.id
    if @accommodation.save
      flash[:notice] = "Successfully created your accommodation."
      render :action => 'show'
    else
      render :action => 'new'
    end
  end

  def edit
    @accommodation = Accommodation.find(params[:id])
  end

  def update
    @accommodation = Accommodation.find(params[:id])
    if @accommodation.update_attributes(params[:accommodation])
      flash[:notice] = "Successfully updated accommodation."
      render :action => 'show'
    else
      render :action => 'edit'
    end
  end

  def destroy
    @accommodation = Accommodation.find(params[:id])
    @accommodation.destroy
    flash[:notice] = "Successfully destroyed accommodation."
    redirect_to :inkeep
  end
end

视图/住宿/ _form.html.erb

<%= form_for @accommodation, :html => {:multipart => true} do |f| %>
  <%= f.error_messages %>
  <p>
    Title<br />
    <%= f.text_field :title, :size => 60 %>
  </p>
  <p>
    Description<br />
    <%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
  </p>
  <p>
    Photo<br />
    <%= f.file_field :photo %>
  </p>
  [... snip ...]
 <p><%= f.submit %></p>
<% end %>

照片

控制器和视图仍然与Rails生成它们时相同。

模型/ photo.erb

class Photo < ActiveRecord::Base
  attr_accessible :image_file_name, :image_content_type, :image_file_size
  belongs_to :accommodation
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "150x150>" }
end

1 个答案:

答案 0 :(得分:2)

要使用回形针创建上传,您需要在您定义的模型上使用您为has_attached_file行提供的名称。在您的情况下,这将导致此视图代码:

<%= form_for @accommodation, :html => { :multipart => true } do |f| %>
  <%= f.fields_for :photo do |photo_fields| %>
    <p>
      Photo<br />
      <%= photo_fields.file_field :image %>
    </p>
  <% end %>
<% end %>

在控制器中:

class AccommodationsController < ApplicationController

  # also protect create and update actions!
  before_filter :login_required, :only => [ :new, :create, :edit, :update ]

  def new
    # always make objects through their owner
    @accommodation = current_user.accommodations.build
    @accommodation.build_photo
  end

  def create
    @accommodation = current_user.accommodations.build(params[:accommodation])
    if @accommodation.save
       # always redirect after successful save/update
       redirect_to @accommodation
    else
       render :new
    end
  end
end

告诉Rails处理嵌套表单:

class Accommodation
  has_one :photo
  accepts_nested_attributes :photo
  attr_accessible :photo_attributes, :title, :description, :etc
end

并确保在照片模型中设置可访问属性:

class Photo
  attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
  has_attached_file :image, :styles => "etc"
end

请务必观看您的日志文件,以发现受attr_accessible保护但仍在您表单中的内容。