Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用回形针将照片添加到博客评论(嵌套模型?)_Ruby On Rails_Paperclip - Fatal编程技术网

Ruby on rails 使用回形针将照片添加到博客评论(嵌套模型?)

Ruby on rails 使用回形针将照片添加到博客评论(嵌套模型?),ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我刚刚开始学习rails,我认为我的问题与嵌套模型有关,但我不确定我做错了什么 基本上,我正在尝试用用户创建的帖子创建一个博客。作为一个典型的博客,我想在每篇文章中添加添加评论的功能。现在,这些图片可以在两个级别上播放:1)我希望用户能够创建帖子,并在帖子上上传图片。现在,一旦这篇文章被创建,我希望评论者能够添加评论,并在相应的文章页面上传不同的图片 我已经创建了带有帖子和评论的博客模型,没有问题。我还添加了回形针,并创建了将“上传图像”选项添加到帖子和评论中的功能。但我只能在显示页面(这是当前

我刚刚开始学习rails,我认为我的问题与嵌套模型有关,但我不确定我做错了什么

基本上,我正在尝试用用户创建的帖子创建一个博客。作为一个典型的博客,我想在每篇文章中添加添加评论的功能。现在,这些图片可以在两个级别上播放:1)我希望用户能够创建帖子,并在帖子上上传图片。现在,一旦这篇文章被创建,我希望评论者能够添加评论,并在相应的文章页面上传不同的图片

我已经创建了带有帖子和评论的博客模型,没有问题。我还添加了回形针,并创建了将“上传图像”选项添加到帖子和评论中的功能。但我只能在显示页面(这是当前所有帖子的提要)和单个帖子页面上显示帖子图像。我试图将图像添加到评论中,但由于某些原因,我得到了一个错误的路径,这会导致“Missing_small”或错误的路径消息

后期模型

 class Post < ActiveRecord::Base

      validates :name,  :presence => true
      validates :title, :presence => true,
                        :length => { :minimum => 5 }

      has_many :comments, :dependent => :destroy
      accepts_nested_attributes_for :comments


     # Paperclip

      has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
                        :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
                        :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension"

      validates_attachment_presence :photo
      validates_attachment_size :photo, :less_than => 5.megabytes
      validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
    end
class Comment < ActiveRecord::Base
  attr_accessor :photo_file_name, :photo_file_size, :photo_content_type

  belongs_to :post

  # Paperclip

    has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
                      :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
                      :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension",
                      :default_url => '/images/missing_:style.png'


    validates_attachment_presence :photo
    validates_attachment_size :photo, :less_than => 5.megabytes
    validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
class Posttrue
验证:title,:presence=>true,
:length=>{:minimum=>5}
有很多:注释,:依赖=>:销毁
接受注释的\u嵌套\u属性\u
#回形针
已附加文件:photo,:styles=>{:small=>“150x150>”,:large=>“360x360”},
:url=>“/system/:class/:attachment/:id/:style\uuu:basename.:extension”,
:path=>“:rails\u root/public/system/:class/:attachment/:id/:style\u:basename.:extension”
验证附件是否存在:照片
验证\u附件\u大小:照片,:小于=>5.MB
验证附件内容类型:照片,内容类型=>['image/jpeg','image/png']
结束
评论模式

 class Post < ActiveRecord::Base

      validates :name,  :presence => true
      validates :title, :presence => true,
                        :length => { :minimum => 5 }

      has_many :comments, :dependent => :destroy
      accepts_nested_attributes_for :comments


     # Paperclip

      has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
                        :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
                        :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension"

      validates_attachment_presence :photo
      validates_attachment_size :photo, :less_than => 5.megabytes
      validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
    end
class Comment < ActiveRecord::Base
  attr_accessor :photo_file_name, :photo_file_size, :photo_content_type

  belongs_to :post

  # Paperclip

    has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
                      :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
                      :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension",
                      :default_url => '/images/missing_:style.png'


    validates_attachment_presence :photo
    validates_attachment_size :photo, :less_than => 5.megabytes
    validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
class注释{:small=>“150x150>”,:large=>“360x360”},
:url=>“/system/:class/:attachment/:id/:style\uuu:basename.:extension”,
:path=>“:rails\u root/public/system/:class/:attachment/:id/:style\u:basename.:extension”,
:default_url=>'/images/missing_u:style.png'
验证附件是否存在:照片
验证\u附件\u大小:照片,:小于=>5.MB
验证附件内容类型:照片,内容类型=>['image/jpeg','image/png']
结束
帖子显示视图

<p id="notice"><%= notice %></p>

<p>
 <% if @post.photo? %>
   <%= image_tag @post.photo.url(:small) %>
 <% else %>
   No attachment available!
 <% end %>
</p>
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Content:</b>
  <%= @post.content %>
</p>

<h2>Comments</h2>
 <%= render @post.comments %>

<h2>Add a comment:</h2>
<%= render "comments/form" %>

<br />

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %> |
<p>
 <%= image_tag comment.photo.url(:small) %>
</p>
<p>
 <b>Commenter:</b>
 <%= comment.commenter %>
</p>

<p>
 <b>Comment:</b>
 <%= comment.body %>
</p>

<p>
 <%= link_to 'Destroy Comment', [comment.post, comment],
                  :confirm => "Are you sure?",
                 :method => :delete %>
</p>

没有附件可用!

姓名:

标题:

内容:

评论 添加评论:
| |
评论视图

<p id="notice"><%= notice %></p>

<p>
 <% if @post.photo? %>
   <%= image_tag @post.photo.url(:small) %>
 <% else %>
   No attachment available!
 <% end %>
</p>
<p>
  <b>Name:</b>
  <%= @post.name %>
</p>

<p>
  <b>Title:</b>
  <%= @post.title %>
</p>

<p>
  <b>Content:</b>
  <%= @post.content %>
</p>

<h2>Comments</h2>
 <%= render @post.comments %>

<h2>Add a comment:</h2>
<%= render "comments/form" %>

<br />

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %> |
<p>
 <%= image_tag comment.photo.url(:small) %>
</p>
<p>
 <b>Commenter:</b>
 <%= comment.commenter %>
</p>

<p>
 <b>Comment:</b>
 <%= comment.body %>
</p>

<p>
 <%= link_to 'Destroy Comment', [comment.post, comment],
                  :confirm => "Are you sure?",
                 :method => :delete %>
</p>

评论人:

评论:

“你确定吗?”, :方法=>:删除%>

如果你还需要什么帮助,请告诉我

提前谢谢,我真的很感激

我希望评论者能够 添加评论并上传不同的内容 相应帖子页面上的图像

我不完全清楚你是否想上传一篇文章或评论的多张图片

为了给你这个选择,我会创建一个照片模型,这个模型有回形针协会

class Photo < ActiveRecord::Base
  belongs_to :post
  has_attached_file :data  
end
class-Photo
然后问题是,这张照片如何同时属于评论和帖子?为此,您可以使用多态关联


这类内容符合您的要求(用帖子替换产品),但您必须使用多态关联对其进行扩展,以便它同时适用于评论和帖子。

Jon-更清楚地说,我希望每个帖子都有最初上传的图像,但在该帖子中,每个评论每个评论只有一个图像。总而言之:原始帖子有一张图片,并嵌套在下面,每个评论有一张图片。我要试一试,看看结果如何。谢谢你的帮助,我会告诉你的。