Hello all,
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...