2012-01-04 51 views
1

我试图通过使用回形针的ajax上传图像。回形针重复的网址错误

我使用qqfileuploader为ajax的东西,它似乎没有一个选项,我可以定义发布请求的参数名称。

从阿贾克斯后发送的参数是

 
qqfile=filename.jpg 

所以在我的模型,我别名qqfile到照片

 
    alias_attribute :qqfile, :photo 
    has_attached_file :photo 
    attr_accessible :title, :photo 

当我上传通过AJAX的文件时,我得到以下错误

 
Parameters: {"qqfile"=>"Penguins.jpg"} 
WARNING: Can't verify CSRF token authenticity 
Creating scope :page. Overwriting existing method User.page. 
    User Load (1.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 
Creating scope :page. Overwriting existing method RoleUser.page. 
Creating scope :page. Overwriting existing method Role.page. 
    Role Load (1.4ms) SELECT `roles`.* FROM `roles` INNER JOIN `role_users` ON `roles`.`id` = `role_users`.`role_id` WHERE `role_users`.`user_id` = 1 
    SQL (0.7ms) BEGIN 
Creating scope :page. Overwriting existing method Task.page. 
[paperclip] Duplicate URL for photo with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Recipe class 

我不知道,如果CSRF令牌将是一个问题,还有就是对网页上的标记,S o也许我只需要发送,我想我可以得到它是一个JavaScript的变量?

但是,重复的URL的处理是什么?我没有正确的别名?由于某种原因,我可以不使用回形针对象吗?

我的控制也很简单

 
def create 
    @recipe = Recipe.new(params[:recipe]) 
    @recipe.author_id=current_user.id 
    if @recipe.save 
     return render :json => @recipe 
    else 
     return render :text => 'an error occured saving the recipe' 
    end 
    end 
+0

你能提供(全部)'has_attached_file'的确切调用吗? – moritz 2012-01-04 14:10:04

+0

@mosch,我添加了has_attached_file和attr_accessible,这只是我所知道的基础知识。 – pedalpete 2012-01-04 14:29:02

回答

2

Rails为基于用户的会话POST事件的安全令牌。如果该令牌缺失或与预期不符,会话将被重置。看到这一点:

http://guides.rubyonrails.org/security.html#csrf-countermeasures

至于重复的URL,你确定你的URL模式是不够具体?它在我看来,如果你上传一个相同的模型实例相同的名称的文件,你会有一个问题。这将有助于查看您的控制器代码。

+0

感谢@yock,我已经在csrf问题上做了最后一点工作,只是看到了你的评论,所以我确实需要以某种方式传递该属性,但还没有完成那项工作。这个过程应该可以运行http://eunikorn.blogspot.com/2011/07/working-with-backbonejs-in-harmony-with.html,但目前还没有。 我已经包含了我的控制器代码,这是非常基本的,所以也不应该是问题的根源,但也许。 – pedalpete 2012-01-04 15:16:35