轨道上的红宝石 - 无法弄清楚为什么回形针插件的照片属性在提交时没有作为参数传入



>我正在尝试在我的应用程序中实现一项功能,用户将消息与图像一起发布。

这类似于www.diasp.org中的内容。

我处理了Rails 2.0.2和Ruby 1.8.7的过时配置,两者都在Ubuntu 10.04操作系统上运行,用于项目特定目的。

最初,我发现在寻找适合上述配置需求的回形针插件时存在问题。

我终于利用了回形针的提交:-

https://github.com/thoughtbot/paperclip/commit/d177c8f838458348a9f4d1fe41918c5bd99a989c .

我已经能够成功地实现一个基本的 POC,而这个插件没有任何问题,并认为是时候在我的项目应用程序中做同样的事情了。

目前面临的问题是,在提交帖子消息时,我无法弄清楚我哪里出了问题,因为是什么阻碍了照片作为参数传递给我正在使用的group_post属性。

我把这一切都放在了部分....代码中接受图像和消息的部分是这样的......

<%if @current_user.is_an_existing_member_of_group(@investor_group)%>
<%form_for  :group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => true, :id => 'new_post'} do |f| -%>
    Start Discussion:<%=f.text_field :message%>
   <%=f.file_field :photo%>
   <%=f.submit "Post" %>
  <%end%>
<div id = "latest_post"> </div>
<%for a in @group_all_posts %>
<%= image_tag a.photo.url %>
<%= a.message %> by <%=  Investor.find(a.post_by).first_name %> <div class ="contentdispgrp" id="style_chck"> <%=  distance_of_time_in_words(a.created_at,Time.now) %> ago </div><br/><br/> <hr/>
<%end%>

实际上尝试将其与POC中的视图文件进行比较,该文件成功为我上传了图像,甚至显示相同。我的 POC 的新 .html.erb 文件如下所示:-

<h1>New post</h1>
<%= error_messages_for :post %>
<% form_for :post, :url => {:action => :create}, :html => { :multipart => true } do |f| %>
  <p>
    <b>Message</b><br />
    <%= f.text_field :message %>
  </p>
  <%= f.file_field :photo%>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>
<%= link_to 'Back', posts_path %>

根据日志作为参数的一部分传递的内容的一瞥看起来像这样,因为 POC 正在运行:-

案例1

   Processing PostsController#create (for 127.0.0.1 at 2011-05-20 15:55:00) [POST]
      Session ID: BAh7ByIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%0ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlZjQ5NWYyNDAxNDVjNmZkOGU5%0AZjM0ZjJjOTZlOTRlM2I%3D--a30d15cac639bde46b5d57218084da993caf094b
      Parameters: {"commit"=>"Create", "post"=>{"photo"=>#<File:/tmp/CGI20110520-5743-u5iauq-0>, "message"=>"abc1"}, "authenticity_token"=>"1c19641bd05adb24157f5f1d04b4c8e072cb7ebe", "action"=>"create", "controller"=>"posts"}
      Post Columns (0.000975)   SHOW FIELDS FROM `posts`
      SQL (0.000199)   BEGIN
      Post Create (0.000332)   INSERT INTO `posts` (`created_at`, `photo_file_size`, `photo_file_name`, `updated_at`, `photo_content_type`, `photo_updated_at`, `message`) VALUES('2011-05-20 15:55:00', 83794, 'Water lilies.jpg', '2011-05-20 15:55:00', 'image/jpeg', NULL, 'abc1')
      SQL (0.059184)   COMMIT
    Redirected to http://localhost:4004/posts/22
    Completed in 0.33966 (2 reqs/sec) | DB: 0.06069 (17%) | 302 Found [http://localhost/posts]

案例2

根据日志作为参数的一部分传递的内容的一瞥看起来像这样,因为项目应用程序正在运行:-

Processing GroupsController#post_message (for 127.0.0.1 at 2011-05-20 15:42:52) [POST]
  Session ID: 0d806dc029197b7db11552a447dd329e
  Parameters: {"group_post"=>{"message"=>"hi5"}, "action"=>"post_message", "id"=>"10", "controller"=>"groups"}
  Investor Columns (0.002125)   SHOW FIELDS FROM `investors`
  Investor Load (0.000173)   SELECT * FROM `investors` WHERE (`investors`.`id` = 397) LIMIT 1
  InvestorGroup Columns (0.001198)   SHOW FIELDS FROM `investor_groups`
  InvestorGroup Load (0.000114)   SELECT * FROM `investor_groups` WHERE (`investor_groups`.`id` = 10) 
  InvestorGroup Load (0.000207)   SELECT investor_groups.* FROM investor_groups INNER JOIN investor_group_members ON investor_groups.id = investor_group_members.investor_group_id WHERE ((investor_group_members.investor_id = 397)) 
  GroupPost Columns (0.002383)   SHOW FIELDS FROM `group_posts`
  SQL (0.000111)   BEGIN
  GroupPost Create (0.000342)   INSERT INTO `group_posts` (`post_by`, `created_at`, `photo_file_size`, `photo_file_name`, `updated_at`, `investor_group_id`, `photo_content_type`, `message`) VALUES(397, '2011-05-20 15:42:53', NULL, NULL, '2011-05-20 15:42:53', 10, NULL, 'hi5')
  SQL (0.069269)   COMMIT
Rendering groups/post_message
  Investor Load (0.000194)   SELECT * FROM `investors` WHERE (`investors`.`id` = 397) 
Completed in 0.13884 (7 reqs/sec) | Rendering: 0.00845 (6%) | DB: 0.07612 (54%) | 200 OK [http://localhost/us/groups/post_message/10]

请注意案例 1案例 2 之间的区别。

我的模型中的代码如下所示:-

class GroupPost < ActiveRecord::Base 
  has_many :group_comments
  belongs_to :investor_group
  validates_presence_of :message
  has_attached_file :photo #,  :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

我想提一下,这是我之前在处理paritals时观察到的奇怪事情......我从未预料到会表现得如此。

以前使用部分实现了自动完成,但是一旦我在此部分中添加了注释,记录了此文件的添加者以及该文件的内容。信不信由你..自动完成停止为我工作...!!.删除评论后不久,它就起作用了。我使用的注释语法<!-- -->

由于我目前不知道的某种原因,我可以期待类似的行为吗...??

我知道我不能指望我正在使用的提交版本有很多东西对我有用,但由于基本功能在 POC 中工作,我想知道是什么阻碍了在我的项目应用程序中工作。

请指导我在哪里可能出错...,它很紧急。

如果您需要代码的任何其他部分,请告诉我。

多谢。。。

在不起作用的代码中,您有:

:html => {:multipart => 'true'}

但它应该是

:html => {:multipart => true}

就像在有效的代码中一样。

每次我遇到这个问题时,都是因为多部分选项的一些拼写错误。

最新更新