I'm still on the hunt for an elegant multi-file upload for Rails.
I just learned about the "input type=”file” multiple"
Does Rails support this? Any examples? tips on how to implement for uploading multiple photos to a photoalbum model in Rails?
Thanks
What you need is that more somenthing like this :
<%= f.file_field :attachment,
:multiple => true %>
Here's a complete, working code snippet for Rails 5:
<%= form_for(#user, html: {multipart: true}) do |f| %>
<%= f.file_field :picture, accept: 'image/png,image/gif,image/jpeg,image/jpg', multiple: true %>
<%= f.submit 'Upload Picture' %>
<% end %>
This is easy in rails. If you're using form_for, do it like so:
form_for(#user, :html => {:multipart => true}) do |f|
If you're doing this with form_tag, it works like so:
form_tag new_user_path, :multipart => true
I hope this helps!
I have used http://www.fyneworks.com/jquery/multiple-file-upload/, and it looks good to me on jQuery-1.7.1 .
Hope it helps.
Related
ive been trying to render a picture from carrierwave. i believe it is uploaded correctly because when i view the page source, i see
<img alt="Photo_44" src="/uploads/user/image/59/Photo_44.jpg" />
however by clicking on that src url, i get
No route matches [GET] "/uploads/user/image/59/Photo_44.jpg"
im using the default settings for carrierwave. the image does correctly get uploaded to my image column in my users table and locally, the path
sasha/Desktop/rails_projects/myproject/public/uploads/user/image/59/Photo_44.jpg
exists as well. however it won't display correctly. ive been following the railscasts
http://railscasts.com/episodes/253-carrierwave-file-uploads?autoplay=true
and reading
https://github.com/jnicklas/carrierwave
but i cant seem to figure out what is wrong. where i upload the image is here
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => { :method => :put, :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<%= render '/shared/fields', object: f.object, f: f %>
<%= f.submit "Save changes", :class => "btn btn-large btn-primary", :style =>"display:block;" %>
<div class="edit_avatar" >
<%= f.file_field :image %>
</div>
<% end %>
and im trying to render it by...
<%= image_tag #user.image_url.to_s %>
what am i doing wrong?
help would be appreciated = )
thank you
Try set config.serve_static_assets = true if you haven't yet, should help.
I'm attempting to use PaperClip and have been following the quick start found on their GitHub page.
I've got the gem install, model & controller set up, and in the view I have:
<% form_for #account, :html => { :multipart => true } do |f| %>
<%= f.file_field :image %>
<% end %>
But nothing displays when the page renders. I've stripped out all CSS to ensure that isn't an issue, but still not luck. Any thoughts on why the file picker isn't displaying? Thanks.
Not sure what version of Rails you are using but I think you forgot the equals sign in the form helper, try:
<%= form_for #account, :html => { :multipart => true } do |f| %>
<%= f.file_field :image %>
<% end %>
I've got two gems that i use and enjoy
gem 'activeadmin'
and
gem "ckeditor"
I'd like for my 'content' field to use ckeditor.
In my past apps, I render ckeditor in a form like this:
<%= form_for #resource do |f| %>
<div class="field">
<%= f.label :content %>
<br />
<%= cktext_area_tag("page_part[content]", #page_part.content) %>
</div>
...
<% end %>
Now i just added activeadmin to my stack and like what i see so far. So, I read that you can customize the form like so by editing the app/admin/#{resource}.rb file:
ActiveAdmin.register NewsItem do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "NewsItem", :multipart => true do
f.input :title
f.input :content
f.input :photo
#NOT WORKING
cktext_area_tag("news_item[content]", #news_item.content)
#NOT WORKING
end
f.buttons
end
end
How can i get this form helper to work in active_admin, and what would i put in place of #news_item.content. #news_item is null... So right now I'm a bit confused.
When I try even witout reference to #news_item like so:
cktext_area_tag("news_item[content]", 'i cant be edited properly')
I still get:
undefined method `cktext_area_tag' for #<ActiveAdmin::DSL:0x00000007e02250>
Any help would be appreciated!
Ok,
Answer was pretty simple.
Taken from active admin's own documentation page: http://activeadmin.info/docs/5-forms.html
ActiveAdmin.register Post do
form :partial => "form"
end
Then I was able to use any form helper tags I wanted to:
<%= javascript_include_tag "/javascripts/ckeditor/ckeditor.js" %>
<%= semantic_form_for [:admin, #news_item], :multipart => true do |f| %>
<%= f.inputs :title, :photo %>
<%= cktext_area_tag("news_item[content]", #news_item.content) %>
<% end %>
You can try
f.template.some_view_method
So I have one form working just fine:
<%= form_for([#document, #comment]) do |f| %>
And then I have another form where I need to include a Carrierwave upload that is like this:
<%= form_for([#document, #comment]), :html => { :multipart => true } do |f| %>
The first one works fine but the second one breaks by pointing to the form definition with the error:
undefined method `comments_path' for #<#<Class:0x0000010475dde8>:0x0000010475a440>
Any ideas? Running Rails 3.0.0 with Ruby 1.9.2p180
<%= form_for([#document, #comment], :html => { :multipart => true }) do |f| %>
<%= form_for (#comment, :url => [#document, #comment], :html => {:multipart => true}) do |f| %>
I believe that will fix it for you.
I have the below form in my view:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :multipart => true) do |f| %>
<label>Select File</label> : <%#= f.file_field :file %>
<%= file_field_tag 'uploadedFile' %>
<%= f.submit 'Upload' %>
</p>
<% end %>
I've commented out the f.file_field but I have tested on both and both give me the same problem. They just return the name of the file and I get a string. methods like .path and .original_filename cause an error.
In my list of parameters I get:
"uploadedFile"=>"test"
(the name of my file is test.txt)
Anyone have any suggestions?
Sorry, I may have misunderstood your original question. It looks like you have an error in your form_for call. It should be:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :html => {:multipart => true}) do |f| %>
You were missing the ":html =>{}" part. Also, you can shorten it down like this:
<%= form_for #filedata, :html => {:multipart => true} do |f| %>
This is happening because the contents of the file aren't stored in the attribute like other fields. File contents are stored in the POST data, and have to be retrieved. Extra steps are required.
This article explains one way to do it manually. But a better approach is to use a plugin like thoughtbot's paperclip to handle this for you.