Ruby on rails 回形针附件未保存到数据库;以表演形式呈现的破碎图像

Ruby on rails 回形针附件未保存到数据库;以表演形式呈现的破碎图像,ruby-on-rails,Ruby On Rails,问题: 虽然“插入和更新”表单在保存回形针附件时没有给出错误信息,但显示表单显示的图像已损坏。我已经尽可能正确地遵循了回形针文档 PS:我在web服务器日志中没有看到:photo属性作为insert语句的一部分 该模型具有以下特点: has_attached_file :photo, :styles => { :thumb=> "100x100#", :small => "150x150>", :medium => "300x300>", :

问题:

虽然“插入和更新”表单在保存回形针附件时没有给出错误信息,但显示表单显示的图像已损坏。我已经尽可能正确地遵循了回形针文档


PS:我在web服务器日志中没有看到:photo属性作为insert语句的一部分

该模型具有以下特点:

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| %>
<%= form_for @teacher, :url => { :action => 'update', :id => @teacher.id}, :html => { :multipart => true } do |f| %>
  <td><%= image_tag @teacher.photo.url %></td>
show.html.erb具有:

<%= form_for @teacher, :url => { :action => 'create'}, :html => { :multipart => true } do |f| %>
<%= form_for @teacher, :url => { :action => 'update', :id => @teacher.id}, :html => { :multipart => true } do |f| %>
  <td><%= image_tag @teacher.photo.url %></td>

为了在
show.html.erb
页面上显示上传的
:照片,您需要在该视图中添加以下代码:

<%= image_tag @teacher.photo.url %>

-或-


其中,将
:style_name
替换为要显示的特定图像样式。例如:
:thumb
:small
:medium
:large
(如
样式
中所述,
选项已附加文件


另外,在
teacher\u参数中
只需允许
:photo
属性,而不允许
:photo\u file\u name

必须在模型文件中执行此操作:

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)

你的show.html.erb有什么?它有。但问题似乎是它甚至没有以某种方式被存储。我在web服务器日志中没有看到:photo属性作为insert语句的一部分。这就是我相信的问题。SHow.heml.erb已经有了。我必须在teacher_参数的私有方法中将:photo_file_name更改为:photo。我做的另一个改变是基于。我在模型中的has_attached_file指令中将其指定为thumb:,small:,medium:,large:。
:thumb=>“100x100#”
thumb:'100x100>“
只是Ruby中哈希的旧语法和新语法。使用新语法总是好的,但旧语法不会破坏代码(仅供参考)。
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)