I would like to have an additional data attribute on an input tag generated by simple_form.
The following does not work:
<%= f.input :date, :as => 'date_picker', :data => {:datepicker => :datepicker} %>
How could this be done? Is it possible at all? As you might have guessed: I am trying to add bootstrap-datepicker to my site without using explicit js to initialize the date picker.
The correct API is:
f.input :date, :as => 'date_picker', :input_html => { :data => {:datepicker => :datepicker} }
For prosperity. On Rails 4.2.5 and Simple Form 3.2.1
The above from #rafaelfranca works for inputs:
f.input :first_name, input_html: { data: { cool_stuff: "yes" } }
If you want to add a data attribute to the simple_form_for helper:
simple_form_for #form, html: { data: { super_cool_key: "secret" } } do |f|
Note the difference: input_html: vs html:
Also, the underscores will automatically get changed to a dash.
Related
So I'm using nested_form (v0.3.2 according to Gemfile.lock) with Rails 3.2.11. I have a Service model with a category field that initially can take on multiple values that can be input via a select dropdown.
The categories are something like: ["J Award", "Z Award", "Other" ]
When I go to save the form fields with a value of J Award (or Z Award) and rerender an editable form, the form redisplays with a value of "Other" for category. Yet if I step into the rails console and look at the category field of the saved service model, it shows "J Award".
What could be causing this? Since nested_form is no longer maintained, should I just give up and handle multiple models in a single form differently?
haml output of rerendered form
= semantic_nested_form_for #service, :url => "/update", :html => { :class => "service", :autocomplete => "off" } do |f|
%h1.page-title Service
.page-wrapper
= render :partial => "shared/error_messages", :locals => { :object => #service }
html output of rerendered form:
<li class="string input required stringish" id="service_category_input"><label class=" label" for="service_category">Category<abbr title="required">*</abbr></label><input id="service_category" maxlength="255" name="service[category]" type="text" value="J Award" />
</li>
= f.inputs do
%h3 Project Information
= f.input :billable, :as => :radio, :collection => { 'Billable' => true, 'Non-billable' => false }, :label => 'Category', :input_html => { :disabled => true }
= f.input :category
= f.input :assigned_consultant, :input_html => { :readonly => true }
= f.input :aims, :input_html => { :readonly => true }
In your html.erb file, I'd change the f.input to f.select:
<%= f.select :category, ["J Award", "Z Award", "Other"], {selected: f.object.category} %>
The selected attribute is self-explanatory. I've never worked with .haml files before, so I guess you would have to convert it somehow.
I am using a jquery plugin for a datetime picker, and it requires the type parameter to be set to 'text'.
So I tried the normal method:
= semantic_form_for #schedule do |f|
= f.input :start_at, :as => :datetime_picker, :input_html => { :class=> 'datetimepicker', :type => 'text' }
But Formtastic is overriding that type and setting it to datetime-local.
<input id="datetimepicker" maxlength="16" name="schedule[start_at]" size="16" step="1" type="datetime-local">
Is there a way to negate Formtastic automatically setting the type, without changing the gem?
So, I realised that I was heading down the wrong path and that if I wanted it be of the text type, it should be a string or text input. Which went against my original thinking that if I want a datetime picker, I should tell formtastic, when I should have only been telling the jquery plugin.
Here is what my haml looks like now:
= semantic_form_for #schedule do |f|
= f.input :start_at, :as => :string, :input_html => { :class => 'datetimepicker', :type => 'text' }
My rails _form code:
<%= simple_form_for #admin_event, :html => { :class => 'main-form' } do |f| %>
<%= f.input :time_of_event, :format => l(:time_of_event, '%d %b. %Y'), :as => :string, :label => 'Hora', :input_html => { :class => 'thin', :placeholder => 'eg: 20:30' } %>
<% end %>
I'm getting the error message:
Cannot convert symbol to string.
How can I set up this _form to always display this field using a time converter so while the database has a full field (2000-01-01 19:30:00.000000), in the forms it would only show 19:30?
Solved this with the following: created the method "time_format" on my ApplicationHelper:
def time_format(datetime)
datetime.strftime('%H:%M') unless datetime.blank?
end
And then on the form:
<%= f.input_field :start_time, :as => :string, :value => time_format(f.object.start_time) %>
Hope this will help you.
You can add attr_accessor to your model (for example formatted_time) and use this field for get fromatted time and set it. Before save you can parse value of #formatted_time and apply it to time_of_event field.
I am using Rails 4 and Ruby 2 with Carrier Wave along with jQuery Mobile 1.3, and a user has a profile which has a logo. I have tested the backend Carrier Wave stuff in the console and it works.
The problem is in the form helper, it doesn't matter what I do, but a file_field will display and let me choose an image, but it does not come through in the params. If I change it to a field that does not exist eg. =f.file_field :field_not_permitted_in_strong_params it does not fall over and a file picker is visible, if I change that to =f.text_field :field_not_permitted_in_strong_params, only then does rails kick in and complain about the field not existing.
So my problem is basically, I can upload a file client side, but it does not get sent through in the form data, or appear in the params hash, and all my other fields work correctly.
Here is a snippet of the form:
= form_for(#business_profile, :html => {:multipart => true}, :url => business_profile_path, :validate => true) do |f|
=image_tag(#business_profile.logo.url, class: 'business-logo')
%div{:data => {:role => 'fieldcontain'}}
=f.file_field :logo
- # Basic Information
%div{:data => {:role => 'collapsible', :collapsed => 'false'}}
%h3
Basic Information
%div{:data => {:role => 'fieldcontain'}}
= f.label :name, 'Business name:'
= f.text_field :name
%div{:data => {:role => 'fieldcontain'}}
= f.label :address, 'Address:'
= f.text_area :address, class: 'address'
OK after spending hours on the matter, the problem is that jQuery Mobile submits forms with Ajax by default, and files cannnot be submitted with Ajax without using plugins etc.
So the solution is to disable the Ajax like this:
= form_for(#business_profile, :url => business_profile_path, :validate => true, :html => { :'data-ajax' => false }) do |f|
I'm trying to create a form with some custom data attributes on the inputs:
<input type="text" data-family="Dinosaurs">
This seemed like a nice clean way to have easy front-end access (haha!) with jquery:
$("[data-family='Dinosaurs']").doSomething()
The problem is I can't get Rails (3.0.3) to render the attribute.
<%= f.text_field :question, :id=>"poll_question", :class=>"BigInput", :style=>"width:98%;", :attributes=>"data-submit_clear='1'" %>
I've tried many permutations to no avail and can't find an example of how to do this. Do I need to modify the text_field helper to support any custom attributes?
Oops. It's just
<%= f.text_field :question, :id=>"poll_question", :class=>"BigInput", :style=>"width:98%;", 'data-submit_clear'=>'1' %>
Rails >3.1 has a handy shortcut for data-attributes like this which most HTML-generating helpers support:
<%= f.text_field :question, :data => { :submit_clear => '1' } %>
It can make things more readable when you have a couple of data attributes, e.g.:
<%= f.text_field :question, :data => { :submit_clear => '1', :more_info => 'Ok', :also => 'this' } %>