我对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但是它不起作用,我知道我可能需要将“照片”添加到允许的params中,但是当我这样做时会出现一个错误,因为我的DB中没有这样的属性,所以我只对上传的图像的扩展名有“扩展名”,因为文件名将是=ID类似于"24.jpg“。我认为我需要允许“照片”参数,并仍然能够创建一个新的人(例如添加“照片”作为一个例外,但我不知道如何做,如果是这样)。不管怎样,有人能帮我吗?
发布于 2013-08-15 04:00:11
我认为你应该添加“照片”到允许的对撞机,否则它将不会放在模型中。
然后将"photo=“移到"photo=”上方,因为如果“photo=”是私有方法,Rails将无法找到属性“图”。
def photo=(file_data)
unless file_data.blank?
@file_data = file_data
self.extension = file_data.original_filename.split('.').last.downcase
end
end
private
........https://stackoverflow.com/questions/18245798
复制相似问题