无法弄清楚为什么回形针插件的照片属性没有作为提交时的参数传递
我正在尝试在我的应用程序中实现一项功能,用户可以在其中发布消息和图像。
这与 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 的 new.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 中工作,我想知道是什么阻碍了它在我的项目应用程序中的工作。
请指导我我可能出错的地方...,很紧急..
如果您需要代码的任何其他部分,请告诉我..
非常感谢...
I am trying to implement a feature in my app where a user posts a message along with an image.
This is something similar to what is there in www.diasp.org
.
I dealing with a pretty much outdated configuration of Rails 2.0.2 and Ruby 1.8.7 , both running on Ubuntu 10.04 OS for project specific purposes.
Initially, I found a problem in finding an appropriate plugin for paperclip which would suit the above configuration needs.
I am finally making use of this commit of paperclip:-
https://github.com/thoughtbot/paperclip/commit/d177c8f838458348a9f4d1fe41918c5bd99a989c .
I have been able to implement a basic POC successfully without any issues whatsoever with this plugin and thought its time to do the same in my project app.
The current problem I am facing is that on submitting a post message, I am unable to figure out where I am going wrong as in terms of what is obstructing the photo to be passed as a params to the group_post attribute that I am making use of.
I am having all this in a partial.... The part of the code that would accept an image and a message is like this...
<%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%>
I actually tried comparing this with the view file in my POC which successfully uploaded images for me and even displayed the same. The new.html.erb file of my POC looks like this:-
<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 %>
A glimpse of what is being passed as part of the params as per the log looks something like this wrt the POC running:-
Case 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]
Case 2
A glimpse of what is being passed as part of the params as per the log looks something like this wrt the project app running:-
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]
Kindly observe the difference between Case 1 and Case 2.
The code in my model looks like this:-
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
I would like to mention this, this something weird I have observed earlier when dealing with paritals.. something i never expected would behave as such..
I had implemented auto complete previously using a partial, but once I added a comment to this partial documenting whom this file was added by and what was this file about.. Believe it or not.. the Auto Complete stopped working for me...!! . It worked soon after removing the comments. The syntax of the comments I used was <!-- -->
Can I expect similar behavior wrt this case for some reason I am currently unaware of...??
I understand I can't expect many things working for me with the commit version I am using , but since the basic functionality is working in the POC, I wonder what is obstructing the same to work in my project app..
Please guide me on where I could be going wrong..., Its urgent..
Kindly let me know if you need any other part of the code..
Thanks a lot...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不起作用的代码中,你有:
但它应该
像在起作用的代码中一样。
每次我遇到这个问题,都是因为多部分选项的一些拼写错误。
In the code that doesn't work, you have:
but it should be
as it is in the code that works.
Every time I've run into this problem, it has been because of some typo around the multipart option.