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 %>
Related
I have a rails app with a model, products that has a nested model, productdocuments. I'm using Carrierwave to upload PDFs, Word Docs, etc .. as the documents.
In my edit.html.erb I have my form field rendering in a partial;
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
And an array of the uploaded docs in a partial:
<%= render #product.productdocuments %>
The issue I'm seeing is that when I upload a file, rails is triggering a render of update.js.erb on the Product NOT the create.js.erb from productdocument This makes it harder to append the partial with the new productdocument.
Any idea how to to trigger create.js.erb from Productdocuments?
I guess in the edit.html.erb there's something like:
<%= form_for #product, remote: true do |f| %>
<%= f.file_field :resource, name: "product[productdocuments_attributes][][resource]", multiple: true, :placeholder => "Resource", id: "product_productdocuments_attributes" %>
<%= f.submit %>
<% end %>
which on submit sends request to the ProductsController, not to ProductDocumentsController as you expect. Since #product is a persisted record, the request goes to update method.
In this case, an additional form for product documents is going solve the problem:
<%= form_for #product.productdocuments.build, remote: true do |f| %>
<%= f.file_field :resource, , multiple: true, :placeholder => "Resource" %>
<%= f.submit %>
<% end %>
#product.productdocuments.build builds a new object, therefore request is going to create method.
Hope it helps.
Tis is my view:
<%= form_for item, :url => comment_item_path(item), :html => {:remote => true, 'portal-transform' => true, :multipart => true} do |f| -%>
<%= f.fields_for :updates, Update.new, :index => nil do |m| -%>
<%= m.text_area :comment %><br />
<%= m.file_field :attachment %>
<% end -%>
<%= f.submit "Comment" %>
<% end -%>
And controller action:
respond_to do |format|
format.js do
render :json => {}
end
end
When I submit the form with only comment (text_area) field entered and keep attachment (file_field) field blank, it render exactly what expected.
But when I submit the form with attachment, it resulted in:
Completed 406 Not Acceptable in 56ms
What went wrong for me? Please guide.
Thanks.
Browsers do not allow file uploads via AJAX for security reasons. If you leave the form's file_field blank however, the form submits normally with no error, which explains the behaviour you are seeing.
To upload files via AJAX in Rails 3, you can use the Remotipart gem.
http://os.alfajango.com/remotipart/
Here is an example usage:
http://thechangelog.com/post/7576700785/remotipart-rails-3-ajax-file-uploads-made-easy
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
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.
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.