问题:
虽然插入和更新表单在保存回形针附件时不会出错,但是显示表单显示图像的图像已损坏。我已尽可能正确地遵循回形针文档。
PS:我没有在网络服务器日志中看到:photo属性作为插入语句的一部分。
该模型有:
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
new.html.erb有:
<%= form_for @teacher, :url => { :action => 'create'}, :html => { :multipart => true } do |f| %>
.... ...
<tr>
<th>Photo</th>
<td><%= f.file_field :photo %></td>
</tr>
edit.html.erb有:
<%= form_for @teacher, :url => { :action => 'update', :id => @teacher.id}, :html => { :multipart => true } do |f| %>
.... ...
<tr>
<th>Photo</th>
<td><%= f.file_field :photo %></td>
</tr>
控制器有:
私有
def teacher_params
params.require(:teacher).permit(:firstname,
:lastname,
:email,
:cellphone,
:username,
:password,
:password_confirmation,
:addr_streetno,
:addr_aptno,
:addr_city,
:addr_state,
:addr_zip,
:photo_file_name) # I had to change it from :photo. Was it the right thing to do. Otherwise, I think I was getting an error.
end
show.html.erb有:
<td><%= image_tag @teacher.photo.url %></td>
答案 0 :(得分:0)
要在:photo
页面上显示上传的show.html.erb
,您需要在该视图中添加以下代码:
<%= image_tag @teacher.photo.url %>
-OR -
<%= image_tag @teacher.photo.url(:style_name) %>
其中,将:style_name
替换为您希望显示的特定图像样式。例如::thumb
,:small
,:medium
,:large
(在[{1}}选项styles
中指定)
此外,在has_attached_file
中,您只需要允许teacher_params
属性,而不是:photo
。
答案 1 :(得分:0)
必须在模型文件中执行此操作:
has_attached_file :photo, :styles => {
thumb: '100x100>',
small: '150x150>',
medium: '300x300>',
large: '400x400>' }
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
此外,在进行严格的参数赋值时,在控制器中进行了此更改:
def teacher_params
params.require(:teacher).permit(:firstname,
:lastname,
:email,
:cellphone,
:username,
:password,
:password_confirmation,
:addr_streetno,
:addr_aptno,
:addr_city,
:addr_state,
:addr_zip,
:photo)