Multi file uploads in rails 4 - ruby-on-rails

I'm having a few problems with multi file uploads in rails 4, I've got it working using carrierwave and polymorphic associations but its creating a file filed for every image in the view. This makes sense as its creating fields for the associated model.
If there is a better or more standard way of doing this in rails 4 I'd be grateful for your advise.
Here is a gist to what I'm doing: https://gist.github.com/lperry65/5805b5f41495f8a820b7
Screen Shot
http://cl.ly/image/3y3w203z3G1f
There seems to be lots of ways to accomplish this, but most of the info I found while googling is for rails 3. It's my intention to use UploadiFive for the front end once I iron out the wrinkles.
http://www.uploadify.com/download/download-uploadifive-standard/

Changing <%= f.fields_for :images do |i| %> to <%= f.fields_for :images[0] do |i| %> stopped the file input from being duplicated for every image. I can't say I'm happy with it but it works for now.
I'll update the gist to reflect any other changes, it's all working now so I'll leave it until I find a better solution.

Related

Need a hand with a rails join

In my app, I have four models - Users > Clients > Jobs > Tasks
I have all of the associations set up fine (as per here --> Advice on RoR database schema and associations), but I'm stuck with a query.
Basically, what I'm doing is:
#potentialjobs = current_user.jobs.where(:status => "potential")
But in the results, I also want to include the client attrs in addition to the job attrs.
Can anyone point me in the right direction?
Thanks.
UPDATE
Appreciate the suggestions below.
I thought this should have been pretty straightforward, but I'm beginning to think the problem must lie somewhere else in my codebase.
Whenever I try to include or join the client model in this query or as a second-order association in the model itself, I get the following error:
uninitialized constant Job::Clients
...and I'm not sure why. So, can anyone shed some light on this for me?
ANOTHER UPDATE
Solved - I had a pluralisation error in one of my models. Gah. Thanks all.
You can access the client's fields through the association in both ways.
<% #potential_jobs.each do |job| %>
<%= job.client.name %>
<% end %>

Filtering Rails database

I have created a database of researchers and the papers they have written in Rails. I need to be able to filter it by author in such a way that information is drawn from the database onto a page in the persons name (for example if there is a researcher called Dr. A. Researcher, I need to be able to go to his page, and that page will be populated with papers that he/she has written automatically). I have been working on this for a while now, and have gone round in circles so many times, Im not exactly sure what I am looking for (though I think its probably going to be AJAX based). Filtering the database seems quite straightforward, but I cant seem to find any information about sending the results of that filter to a specific page.
As I say, Im not totally sure what Im looking for here, so someone may have had a similar issue that has already been answered. If that is the case, I apologise. Any help anyone has about this would be very gratefully received.
Cheers
Seems like you would just want to use the show action.
In your controllers/researchers.rb controller add
def show
#reseacher = Reseacher.find(params[:id])
end
or possibly
def show
#reseacher = Reseacher.find(params[:id], include: [:papers])
end
then in your views/show.html.erb view you can just add
<% #reseacher.papers.each do |p| %>
<%= p.title %>
<%= p.otherattribute %>
<% end %>
Also you will need to ensure your relationships are setup correctly in your model
in models/researcher.rb
add
has_many :papers
to the Researcher Class
and in models/papers.rb
add
belongs_to :researcher
to the Paper Class
localhost:3000/reseachers/1 where 1 is the id of the researcher should then return the papers for that researcher

Adding records via check boxes in a nested form

My form is nested so that a Cart has Products, which can have Features. I wish to be able to add features to products via checkboxes.
Upon submit, rails is creating the Cart record, but failing to use "accepts_nested_attributes_for" to complete the addition of nested records.
My form is very straight forward, and I'm not receiving any errors, it's just ignoring the fields. My fields look like this (within the form helper)
= f.fields_for :feature_line_items do |builder|
= builder.check_box :id
= builder.label :id, "Feature Label"
Thanks in advance, I'm kind of doubting this is even possible, and probably need to rethink my data architecture.
Try this episode in railscasts. Also I suggest using simple_form gem, it makes forms programming very simple and straight forward.
I hope that helped.
Take a look :
http://asciicasts.com/episodes/17-habtm-checkboxes
http://ramblings.gibberishcode.net/archives/rails-has-and-belongs-to-many-habtm-demystified/17

Best approach to multiple file uploads in Ruby on Rails

I have asked a similar question here with unsuccessful answers:
Uploadify + Paperclip + Rails nested association before_save
So i will reformulate my question:
What's the best approach in Rails to upload multiple files at once and associate them to an object that is not yet saved? (for example a model (girl) application form that is saved to the database when the create action is been complete (the save button is pressed).
Many ideas come to my mind (save the object by ajax before he try to upload the images, add a token to images and then add the ID of the model after the model object is saved) but im pretty sure many people have done this and there's a common way or best approach to do it.
Thank you in advance!
Martin.
I use this with one of my rails 3 apps:
= form_for :import_csv, :url => {:action => :import_csv}, :html => {:multipart => true} do |f|
= f.file_field :csv
= f.submit 'Upload CSV'
This creates a temporary file which can be retrieved with
CSV.open(params[:import_csv][:csv].tempfile.path)
I see no reason why this could not be extended to multiple uploads, and accessed at params[:import_csv][:whatever]
Note** the handling of tempfiles was changed slightly in rails 3.0.3, which is why the above code uses .tempfile.path which was not required in previous versions.
Over a year ago I was faced with a similar problem and could not find a ready solution therefore has made as follows:
1.Using SWFUpload upload images to an "store_image" action that stores, resizes, ..., and returns path to the thumbnail and the IDs of uploaded image.
2. Using JS put image IDs in hidden field(s) value. I used single field with value like "2312111:3231231:323212".
3. When create a "master" object, find the uploaded images by their IDs and establish their relation with the subject.
Also garbage collector removes unrelated images created over 3 days ago. The garbage collector runs by cron every day.
As for me, it is the most elegant solution.
__
Sorry for my bad English

way of implementing links in rails

I have a project I am doing in rails. I want to implement this sort of links
{user} has {action} on {file} in {project}
each of the words wrapped by curly braces are entities in my system (models). how do I implement saving these changes in the project and how do I get all of the changes from the database and display them to the user?
I am using rails 2.3.8 if it matters
example of the links I need to display (image)
There are some plugins available to keep track of activities in models:
https://github.com/grosser/record_activities
https://github.com/linkingpaths/acts_as_scribe
https://github.com/face/activity_streams
I normally use acts_as_scribe, it's the simplest of them.
I would recommend acts_as_audited. It works very well. It saves all your changes on a model in the form of a hash, so using it becomes as easy as
audit = Audit.first #just for example
In your view
<%= link_to User.find(audit.user_id), user(:id => audit.user_id) %> has
<%= audit.action %>
Of course you will have to customize how your messages will finally appear. And of course its better not to use find methods in your view. I've used it here just for illustration purposes.

Resources