1

我是 Rails 的新手,所以我决定开始学习根据一本书做练习,但它适用于 rails 3,所以毫无疑问有些东西不适用于 rails 4。我试图根据书中的代码构建简单的图像上传器(无需告诉我用其他方式做到这一点有多容易)所以我有一个看法:

<%= form_for(@person, :html => { :multipart => true }) do |f|  %>
<% if @person.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2>

  <ul>
  <% @person.errors.full_messages.each do |msg| %>
    <li><% xxx=msg.split %>
        <% xxx.shift %>
        <%= p xxx.join(" ")%>
    </li>
  <% end %>
  </ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :secret %><br>
<%= f.password_field :secret %>
</div>
<div class="field">
<p>
<%= f.label :country %><br />
<%= f.select :country, [ ['Canada', 'Canada'],['Mexico', 'Mexico'],['United Kingdom',    'UK'],['United States of America', 'USA'] ]%>
</p>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :can_send_email %><br>
<%= f.check_box :can_send_email %>
</div>
<div class="field">
<%= f.label :graduation_year %><br>
<%= f.number_field :graduation_year %>
</div>
<div class="field">
<%= f.label :body_temperature %><br>
<%= f.text_field :body_temperature %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :birthday %><br>
<%= f.date_select :birthday %>
</div>
<div class="field">
<%= f.label :favourite_time %><br>
<%= f.time_select :favourite_time %>
</div>
<div class="field">
<%= f.label :photo %><br />
<%= f.file_field :photo %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

用于创建的控制器的一部分:

    def create
    @person = Person.new(person_params)
    respond_to do |format|
    if @person.save
    format.html { redirect_to @person, notice: 'Person was successfully created.' }
    format.json { render action: 'show', status: :created, location: @person }
    else
    format.html { render action: 'new' }
    format.json { render json: @person.errors, status: :unprocessable_entity }
    end
    end
    end
    def person_params
  params.require(:person).permit(:name, :secret, :country, :email, :description, :can_send_email, :graduation_year, :body_temperature, :price, :birthday, :favourite_time)
end

和模型的一部分:

    after_save :store_photo


     private

    PHOTO_STORE = File.join Rails.root, 'public', 'photo_store'

    def store_photo
    if @file_data    
FileUtils.mkdir_p PHOTO_STORE
File.open(photo_filename, 'wb') do |f|
  f.write(@file_data.read)
    end    
   @file_data = nil
   end
    end
   def photo=(file_data)
   unless file_data.blank?   
  @file_data = file_data
self.extension = file_data.original_filename.split('.').last.downcase
    end
    end


    def photo_filename
    File.join PHOTO_STORE, "#{id}.#{extension}"
    end
    def photo_path
    "/photo_store/#{id}.#{extension}"
    end
    end

但这不起作用,我知道我可能需要将“照片”添加到允许的参数中,但是当我这样做时会出现错误,因为我的数据库中没有这样的属性,我只有上传图像的“扩展名” ' 扩展名,因为文件名将是 = ID,例如“24.jpg”。我想我需要允许“照片”参数并且仍然能够创建一个新人(比如添加“照片”作为例外或其他东西,但我不知道如何做到这一点,如果是这样的话)。无论如何,有人可以帮助我吗?

4

1 回答 1

2

我认为您应该将“照片”添加到允许的参数中,否则它不会被放入模型中。

然后将“photo=”移到“private”上面,因为如果“photo=”是私有方法,Rails 将无法找到属性“photo”

def photo=(file_data)
    unless file_data.blank?
        @file_data = file_data
        self.extension = file_data.original_filename.split('.').last.downcase
    end
end

private

........
于 2013-08-15T04:00:11.780 回答