how to add Active Admin form input :html {multiple => ""}? - ruby-on-rails

ActiveAdmin form:
my problem: I can not get a form with multiple choice files(
I tried:
ActiveAdmin.register Foto do:
form :html => { :multipart => true } do |f|
f.inputs "Upload" do
f.input :foto, :as => :file
end
f.actions
end
end
this does not work, then I made a simple html page with two forms:
<!DOCTYPE doctype html>
<html>
<head>
</head>
<body>
<!--**not** work multiple choice files-->
<form accept-charset="UTF-8" action="#" enctype="multipart/form-data" method="post">
<input id="image" name="image" type="file"/>
</form>
<!--**great** work multiple choice files-->
<form accept-charset="UTF-8" action="#" method="post">
<input id="image" name="image" type="file" multiple=""/>
</form>
</body>
</html>
Question: how to add a property to multiple input field ?
I tried:
f.input :foto, :as => :file, :html => {:multiple => ""}
f.input :foto, :as => :file, :html => {:multiple => ""}
f.input :foto, :as => :file, :html => {"multiple" => "multiple"}
f.input :foto, :as => :file, :html => {:multiple => :multiple}
this does not work
help me, please.

You should use
f.input :foto, as: :file, input_html: { multiple: true }

Related

Rails jquery datepicker can't find html

I'm using the jquery-ui datepicker in a Rails app. I have 2 forms for entering data. One is a regular Rails view page and the other is a Bootstrap modal. The modal one isn't working.
This is the coffeescript (I'm assuming this will work for both):
$("#expense_exp_date").datepicker dateFormat: "yy-mm-dd"
This is the Rails view code and corresponding html:
<%= f.input :exp_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => {:value => Date.today.to_s} %>
<input class="string required hasDatepicker" id="expense_exp_date" name="expense[exp_date]" size="50" type="text" value="2013-01-21">
This is the Bootstrap modal code and corresponding html:
<%= f.input :exp_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => {:value => Date.today.to_s}, :input_html => {:style => 'width: 180px'} %>
<input class="string required" id="expense_exp_date" name="expense[exp_date]" size="50" style="width: 180px" type="text">
Why is "hasDatepicker" missing from the modal html?
Thanks!
Try to pass the class option inside the input_html Option hash:
<%= f.input :exp_date, :as => :string, :label => 'Date',
:input_html => {
:value => Date.today.to_s,
:class => 'calendar hasDatepicker'} %>

accepts_nested_attributes_for with complex association

I'm using accepts_nested_attributes_for in one of my Rails app. i have three models :album has_many :photo and each :photo has_and_belongs_to_many :tag. User can add more photo in album using paperclip and jquery.multifile.js. every thing working fine but the main problem is the hash is not properly create.
album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}
but i need.
album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}
My view is: please consider the gallery => album and attachment => photo
<%= form_for(#gallery, :html => {:multipart => true}) do |f| %>
<div id = "gallery_name_formField">
<%= f.label "Gallery Name:" %><br />
<%= f.text_field :name, :name => "gallery[name]" %><br />
</div>
<div id = "gallery_body_formField">
<%= f.label "Gallery Description:" %><br />
<%= f.text_area :body, :name => "gallery[body]" %><br />
</div>
<%= f.fields_for :attachments do |af| %>
<div id = "gallery_file_formField">
<%= af.label "Select The Attachment:" %><br />
<%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
</div>
<div id="tags">
<%= af.fields_for :tags do |tf| %>
<%= tf.label :name, "Tag:" %><br />
<%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
<% end %>
</div>
<% end %>
<div class="attachment_submit_formField">
<%= f.submit(:id => "create_gallery") %>
</div>
<% end %>
Have you tried to use nested form gem? Give it a try
Its very straightforward way to have multiple nested forms.
It works for me.

client_side_validations 'Cannot read property 'user[email]' of undefined'

So I am trying to get client_side_validations gem working and when I tab out of my email field, I get this error:
Uncaught TypeError: Cannot read property 'user[email]' of undefined
This is my form helper:
<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true) do |f| %>
<%= f.email_field :email, :id => "form-email", :placeholder => "your-email#address.com", :input_html => {:autofocus => true}, :validate => true %>
<%= f.submit :label => "Submit", :value => "Sign Me Up!" %>
<div class="ajax-response"><%= devise_error_messages! %></div>
<% end %>
Which generates this HTML:
<form accept-charset="UTF-8" action="/users" class="new_user" data-validate="true" id="new_user" method="post" novalidate="novalidate">
<input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="9JdqaiushdauhsdioauhdoiuhNLSAVCGrc+PLJAQ=" />
<input data-validate="true" id="form-email" input_html="{:autofocus=>true}" name="user[email]" placeholder="your-email#address.com" size="30" type="email" />
<input label="Submit" name="commit" type="submit" value="Sign Me Up!" />
<div class="ajax-response"></div>
</form>
Edit 1: Even when I change the form helper from f.email_field :email, to f.text_field :email...I am still getting this error.
So I found the solution, it seems that either client_side_validations or devise are altering the id of the form - which would break the JS validation.
So the solution is to enforce the id of the form, so my form_for helper now looks like this:
<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource), :validate => true, :html => { :id => 'user_new' }) do |f| %>
I found this solution here.
Have a look at the following #263 Client Side Validations. This should help.

Rails file_field helper doesn't return anything

I'm using Simple Form. I have a form for creating new items, and a form for editing existing ones. I also have two file fields for every item. Thing that bugs me is that file fields are displayed fine when creating new item, but then they are not generated at all when editing an existing item.
I had this perfectly working in Rails 3.0, now doesn't work on Rails 3.2.1.
The form:
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.input :title, :input_html => { :maxlength => 35 } %>
<%= f.input :description, :input_html => { :maxlength => 450 } %>
<%= f.input :secure_details, :placeholder => "Serial numbers and other stuff that will remain private", :input_html => { :maxlength => 450 } %>
<%= f.association :bookmark, :collection => current_user.bookmarks(:order => :position), :include_blank => false %>
<%= f.input :warranty_until, :as => :string, :input_html => { :id =>'datepicker2' } %>
<div class="image_attachment">
<div class="attachment_text">
Update item photo<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
</div>
<div class="image_attachment">
<div class="attachment_text">
Update receipt<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
</div>
</div>
<%= f.input :public, :label => "My friends can see this item", :input_html => { :class => "right" } %>
<%= f.input :giveaway, :label => "Mark as giveaway", :input_html => { :class => "right" } %>
<div class="margin_r margin_t">
<%= f.button :submit, :class => 'small_button white right' %>
</div>
<% end %>
Basically this part of code doesn't work:
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
The generated HTML is just empty div.
The very same code works when creating a new item, but doesn't work when editing existing one.
Both Assets and Receipts are using Paperclip for storing images. Here is a code for Asset class:
class Asset < ActiveRecord::Base
belongs_to :item
has_attached_file :photo,
:styles => {
:thumb => "80x80#",
:small => "150x150>" }
validates_attachment_size :photo, :less_than => 550.kilobytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Maybe you forgot add this line of code in your Item model:
accepts_nested_attributes_for :receipts, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true
And add '=':
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
<%= f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>

Rails - f.file_field says 'no chosen file after selecting and submitting'

I just followed railscast 134 to install paperclip into my rails 3.0.1 app. I did everything necessary but when I select a file and press submit on my form, it reloads the page and says "no file chosen"
This is in my events model:
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/events/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/events/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg',
'image/png',
'image/jpg']
and my form:
<h1>Edit Event</h1>
<%= form_for #event, :html => { :multipart => true } do |f| %>
<div class="actions">
<%= f.label :name %><br />
<%= f.text_field :name %><br /><br />
<%= f.label :description %><br />
<%= f.text_field :description %><br /><br />
<%= f.label :event_date %><br />
<%= text_field_tag :event_date, #event.event_date.try(:strftime, "%m/%d/%Y at %I:%M%p"), :size=>30 %><br /><br />
<%= f.label :location %><br />
<%= f.text_field :location %><br /><br />
<%= f.file_field :photo %><br /><br />
<%= f.submit "Update" %>
</div>
<% end %>
can't quite figure it out. thanks!
EDIT: some paperclip code in my event model:
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/events/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/events/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg',
'image/png',
'image/jpg']
and some debug output:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"hScfg9tx/4gBOHrtg+u8MB+QeUWS1sKtlcbnzK8YmRI=", "event"=>{"name"=>"Park", "description"=>"Swimming", "location"=>"Golden Gate Park", "photo"=>#<File:/var/folders/lm/5svv8x4s4d50yhw_h05qz7mm0000gn/T/RackMultipart20111023-6938-8xsv50>}, "event_date"=>"10/10/2011 at 02:00PM", "commit"=>"Update", "action"=>"update", "controller"=>"events", "id"=>"3"}
Pretty sure that's normal behavior for a file_field, It only displays the file name when you select it to upload, it doesn't show the existing file saved in the Database.
If you want to display and access uploaded content try something like this in your view:
<%= link_to(image_tag(#event.photo.url(:small)), #event.photo.url) %>
Hope this helps

Resources