新手问题 rails_kindeditor 删除内容中的图片

frank · 2013年08月01日 · 最后由 Macrow 回复于 2015年03月27日 · 4762 次阅读

问题描述 我用 rails_kindeditor,做富文本编辑器。当上传图片的时候,会把图片存储在本地磁盘上。当我想把该条记录删除时。我也想把该条记录中上传的图片给删除。我应该怎么操作。 我应该如何拿到这个图片路径?

kindeditor 本身没有管理或者删除文件地接口,删除文件得自己来实现。

rails_kindeditor 可以存储上传文件的信息,至于删除功能要自己实现,下次有时间把这个需求和功能加进去。

今天加班已把所描述的功能加上,详细说明见 ReadMe。

https://github.com/Macrow/rails_kindeditor

@Macrow :owner_id => @xxx.id 这个功能好像不行

@tailang

1.需要数据库记录

rails generate rails_kindeditor:migration
rake db:migrate

2.指定 owner_id

<%= f.kindeditor :content, :owner_id => @article.id  %>

3.在你自己的模型里加入 has_many_kindeditor_assets

class Article < ActiveRecord::Base
  has_many_kindeditor_assets :attachments, :dependent => :destroy
  # has_many_kindeditor_assets :attachments, :dependent => :nullify
  # has_many_kindeditor_assets :your_name, :dependent => :destroy
end

这三个步骤都完成了吗?

@Macrow 嗯嗯 按照 README 来的

<%= f.kindeditor :comment, :simple_mode => true, :owner_id => @comment.id, :width => 700, :height => 200 %>

:owner_id 的参数位置应该无关吧?

@tailang 参数是通过 hash 传递的,顺序无关。

你的 log 贴出来看看吧,或者看看代码。

我觉得是你的 Comment 没有创建导致的,这里有一个先后逻辑问题,因为 comment 没有创建,所以你的@comment.id本身是 nil。

在 README 里面我有说明,是不是 没注意到?

# 警告: @article应该事先被创建,@article.id不应该是空的。

@Macrow 下面的是和我的 post 有关的 model

class Post < ActiveRecord::Base
  attr_accessible :body, :title, :tag_list

  has_many_kindeditor_assets :attachments, :dependent => :destroy 
  acts_as_taggable
  acts_as_commentable
  default_scope :order => 'id DESC'

  validates :title, :presence => true
  validates :body, :presence => true
end

form

<div class="field" >
   <%= f.label :body, "正文" %>
   <%= f.kindeditor :body, :simple_mode => true, :owner_id => @post.id, :width => 700, :height => 400 %><br>
 </div>

show

<%= safe @post.body %> #safe是一个html_safe的方法

下面是删除 post 时与 kindeditor 有关的

Started DELETE "/posts/14" for 127.0.0.1 at 2013-08-12 22:07:23 +0800
Processing by PostsController#destroy as HTML
  Parameters: {"authenticity_token"=>"zjuURXhL98MnxDJIEEUcPNsF0L7ZeqRKeKhW1/uRW7w=", "id"=>"14"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Post Load (0.2ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? ORDER BY id DESC LIMIT 1  [["id", "14"]]
   (0.1ms)  begin transaction
  Kindeditor::Asset Load (0.2ms)  SELECT "kindeditor_assets".* FROM "kindeditor_assets" WHERE "kindeditor_assets"."owner_id" = 14
  ActsAsTaggableOn::Tagging Load (0.2ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = 14 AND "taggings"."taggable_type" = 'Post'
  SQL (0.3ms)  DELETE FROM "taggings" WHERE "taggings"."id" = ?  [["id", 15]]
  ActsAsTaggableOn::Tagging Load (0.2ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = 14 AND "taggings"."taggable_type" = 'Post' AND (taggings.context = ('tags'))
  Comment Load (0.6ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = 14 AND "comments"."commentable_type" = 'Post' ORDER BY created_at DESC
  SQL (0.4ms)  DELETE FROM "posts" WHERE "posts"."id" = ?  [["id", 14]]
   (179.7ms)  commit transaction
Redirected to http://localhost:3000/posts
Completed 302 Found in 257ms (ActiveRecord: 182.0ms)

删除 post 后对应的图片还在 public 下

@tailang 从代码上看,应该是没有问题的。

分析 log,应该是 kindeditor 创建 assets 的时候,owner_id 没有写入。

你通过 rails c 来看看所有 assets 里面有没有 owner_id 为 14 的:

rails c
Kindeditor::Asset.all.map(&:owner_id)

@Macrow 恩 没写入

irb(main):002:0> Kindeditor::Asset.all.map(&:owner_id)
  Kindeditor::Asset Load (0.5ms)  SELECT "kindeditor_assets".* FROM "kindeditor_assets" 
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

@tailang 那你能确定你的 form 里面@post.id不为空吗?

@Macrow 嗯嗯,不好意思啊,我查了一下,是 post.id 为空,不过我也有个疑问 我在 controller 中 action new

def new
    @post = Post.new #为<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil>,id为空,为了@post.id不为空的话,new的时候就给post.id赋值吗?
end

@tailang 我在 6 楼不是说了吗,这里有一个先后的问题,在 action new 里面,@post还没有创建,而这个时候上传图片的时候就完成了附件的创建过程,所以@post应该首先创建,然后再可以创建属于@post的附件。

如果非要加入 owner_id 的话,我觉得可以指定为@user.id,或者先让用户创建@post

不推荐你说的加入 id 的方法,即便你通过 Model.maximum(:id).next 或者 Model.last.id+1 之类的方法预测了该 Model 的 id,用户也有可能上传附件后选择取消,而且,在你创建 Model 的时候,id 可能已经增长了好几个了(其他用户也在创建)。

@Macrow 额,这个我明白,刚刚也试过了,先创建 post,在重新去添加图片。但是这样实现不论是创建带图片的文章还是评论有点麻烦(其实我对这个功能不是一定要有,只是测一下)。前辈不好意思,刚刚没仔细看 6 楼的回复,浪费你宝贵时间,谢谢啦

不客气,共同学习。

#14 楼 @Macrow 不停的使用 kindeditor 上传图片,可以无限的上传吗?

@feichan 理论上可以这么做,但如果上传文件涉及到安全问题,或者要考虑每一名用户可上传量的控制问题,就需要更深入的定制和编码了。

@Macrow 你好,我用 kindeditor 遇到一个问题想请教一下您,因为我用了 turbolinks,所以我加了'data-no-turbolink' => true 但是要按一下 F5 刷新编辑器才出来,后来我在 coffeescript code 里加入那段代码,不用刷新就能出来,但是和 kindeditor_assets 关联不起来,我把数据删了,但图片删不掉, (PS 不加那段代码是可以删图片的)请问是什么原因?知道吗?谢谢

@osbron 无法删除 assets 和 turbolinks 应该是没有关系的,应该是你的 owern_id 没有设置正确,请仔细检查一下。

@Macrow 刚发现首次点进页面时上传图片 无法删除 assets,我按 F5 刷新一下再上传图片就可以删除 assets 一直搞不懂

@Macrow
这是我的 action def article_create @article = Article.new() @article.site = @site @article.title = '' @article.save! end

<%= f.kindeditor :content, :owner_id => @article.id,:width => 700,:height => 300 %>

提交 action: def article_create_action if request.post? @article = Article.find_by_id(params[:id]) @article.update_attributes(article_params) redirect_to :action => 'articles' end end

@Macrow

<%= f.kindeditor :content, :owner_id => @article.id,:width => 700,:height => 300 %>

提交 action: def article_create_action if request.post? @article = Article.find_by_id(params[:id]) @article.update_attributes(article_params) redirect_to :action => 'articles' end end

@Macrow 找到问题了 就是没加载 js 要按 F5 刷新加载 JS
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> 我是通过这句话引入 js 的 虽然知道问题所在,但还是不知到怎样解决,请问你知道吗?

#2 楼 @Macrow 我要给 kindeditor_assets 添加 owner_type 属性, 1.我给表添加了列, 2.我在 f.kindeditor 后添加了:owner_type => xx.class.to_s, 3,我在 assets_controller 的 create 方法里添加了接收 params[:owner_type] 的代码, 为什我 debug 看不到传 owner_type 进来? 谢谢指导!

#24 楼 @lizanle 这种情况建议你在 assets_controller 的 create 的方法里进行调试,可以用 logger 输出 params 看看有没有你需要传递进来的参数。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号