I want to add a custom css class to my date_selects in Rails 4.2.1
But the html_options tag has no effect.
code looks like this:
<%= raw t("settings.pause", :pause_starts_on => f.date_select(:pause_starts_on, :include_blank => true, :order => [:month, :day, :year], :start_year => Time.now.year, :html_options => {:class => "form-control"}).force_encoding("UTF-8"),
:pause_ends_on => f.date_select(:pause_ends_on, :include_blank => true, :order => [:month, :day, :year], :start_year => Time.now.year, :html_options => {:class => "form-control"}).force_encoding("UTF-8"))
%>
Try :html => {:class => "form-control"} without the _options
Related
#tech_data = TagNode.first outputs below object from the controller .
p #tech_data
#<TagNode _id: 5119dcf74054448e576f3392, parent_id: nil, parent_ids: [], name: "categories", path: "categories", _type: "TagNode">
I have a form which has the fields name, _type, and category. But my object doesn't have the field category.
Here's my form
= simple_form_for #tech_data, as: :techtags, :url => view_techtags_technologies_path, :remote => true,:method => :get,:html => {:id => "upsert_techtags",:data => {:spinner => "#tech-ui"}} do |f|
%label.pull-left Type
= f.input :_type, :collection => tech_types, :label => false, :input_html => {:class =>"chzn-select", :data => {:placeholder => "Select Technology Type"}, :multiple => false}
%label.pull-left Name
= f.input :name, :type=>"text", :required => true, :label => false, :placeholder =>"Type the New Technology", :input_html => {:style => "width:265px;margin-bottom:0;"}
%label.pull-left Category
= f.input :category, :collection => [1,2,3,4], :label => false, :input_html => {:class =>"chzn-select", :disabled=>"true",:data => {:placeholder => "Type or select category"}, :multiple => false}
= f.submit "Create", :class => "btn btn-primary pull-right"
My form has an additional field that my object doesn't have. I get an error when the form is loaded, as I don't have category field in my object.
How can I select the category value in the form without adding this field to the model?
You need something like attr_accessor in Ruby,What is attr_accessor in Ruby?
Please include following in your model,which will make to get values from form to controller.
attr_accessor :category
I have the following select tag
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:required => true}, {:class => "multiselect", :multiple => true} %>
I try to add :required => true to it, the view renders but :required => true doesn't work!.
Quite a bit late, but for anyone looking, checking at the reference, should go explicitly as the hash, so ruby interpreter doesn't get confused:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
For this special case:
<%= f.select :id,
User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]},
{:prompt => 'Select something'},
{:required => true, :class => "multiselect", :multiple => true} %>
Try this:
<%= f.select :id, User.find(:all, :conditions => ["manager = ?", false]).collect {|u| [u.username, u.id]}, {:multiple => true}, :class => "multiselect", :required => true %>
I have the typical rails _form generated by scaffold for a member model. For the param :state I am using a select tag:
<%= f.select :state, options_for_select(us_states), { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
Using a helper method (us states):
def us_states
[
['AK', 'AK'],
['AL', 'AL'], #etc
And for the param "member_since" I am using the select_year helper:
<%= select_year(0, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
Now both of these selects work to create a new record, but neither field is pre-filled in the edit record view. Any thoughts?
<%= f.select :state, us_states, { :include_blank=>true, :prompt => 'State' }, { :class => 'form-control' } %>
and
<%= select_year(f.object.member_since, {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
UPDATE:
Since member_since is a string, you will need to convert it to date:
<%= select_year(Date.new(f.object.member_since.to_i), {:start_year => 2013, :end_year => 1920, :field_name => 'member_since', :prompt => 'Choose year', prefix: :member}, {:class => "form-control"} ) %>
I have the following use of date_select..
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html => {:class => "select birthday"} %>
But the class does not show up in the html..
<select id="profile_birthday_2i" name="profile[birthday(2i)]">
<select id="profile_birthday_3i" name="profile[birthday(3i)]">
I also tried..
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :class => "select birthday" %>
That did not work either. Any ideas?
The HTML options are a fourth argument to the date_select method, rather than being a key in the third argument.
From the documentation:
date_select(object_name, method, options = {}, html_options = {})
So you'd want:
f.date_select :birthday, { :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' } }, {:class => "select birthday"}
You need to use html_options, not html to specify the class.
I believe this will work, though I've not tested it.
<%= f.date_select :birthday, :order => [:month, :day], :prompt => { :day => 'Select day', :month => 'Select month' }, :html_options => {:class => "select birthday"} %>
See the API description here:
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
Note: The docs say:
If anything is passed in the html_options hash it will be applied to every select tag in the set.
So make sure you expect that class to show up on each element.
I followed the tutorial http://railscasts.com/episodes/253-carrierwave-file-uploads?view=asciicast and I've got my nested form setup with the attached inside a nested form below. Everything works fine but the image isn't being uploaded / storing the filename in the db.
<%= simple_nested_form_for(#order) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<label> <%= link_to 'New Customer', new_customer_path %> <small>or</small></label><div class="clear"></div>
<%= f.association :customer, :label =>'Existing Customer', :include_blank => false %>
<%= f.input :due_date, :as => :date, :start_year => Date.today.year, :start_day => Date.today.day, :stary_month => Date.today.month, :order => [:month, :day, :year], :input_html => { :class => 'date' } %>
<%= f.input :sales_tax, :input_html => { :class => 'text', :value => current_user.sales_tax, :onChange=>"itemcalculate()", :id => 'invoice-salestax' }, :hint => '%' %>
<%= f.input :discount, :input_html => { :class => 'text', :onChange=>"itemcalculate()", :id => 'invoice-discount' }, :placeholder => '$30', :label => 'Discount' %>
<%= f.fields_for :lineitems do |item| %>
<div id='size'>
<label style="margin-top:0 !important;">Details</label>
<%= item.input :product_name, :input_html => { :class => 'text' }, :label => false, :wrapper_html => { :class => 'detail-wrapper' }, :input_html => { :class => 'details'}, :hint => 'Product Name', :placeholder => 'Gildan 2000' %>
<%= item.input :color, :input_html => { :class => 'text' }, :label => false, :wrapper_html => { :class => 'detail-wrapper' }, :input_html => { :class => 'details'}, :hint => 'Product Color', :placeholder => 'Blue' %>
<%= item.input :price_per, :input_html => { :class => 'text details', :onChange=>"itemcalculate()", :id => 'invoice-priceper' }, :label => false, :wrapper_html => { :class => 'detail-wrapper' }, :hint => 'Price per', :placeholder => "4.50", :required => true %>
<%= item.input :extra_fee, :input_html => { :class => 'text details', :onChange=>"itemcalculate()", :id => 'invoice-extrafee' }, :label => false, :wrapper_html => { :class => 'detail-wrapper', :value => '0' }, :hint => 'Extra fee', :required => true %>
<div class='clear'></div>
<label>Sizes</label>
<%= item.input :xxs, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-xxs' }, :hint=> 'xxs', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<%= item.input :xs, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-xs' }, :hint=> 'xs', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<%= item.input :s, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-s' }, :hint=> 's' , :label => false, :wrapper_html => { :class => 'size-wrapper' }%>
<%= item.input :m, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-m' }, :hint=> 'm', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<%= item.input :l, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-l' }, :hint=> 'l', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<%= item.input :xl, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-xl' }, :hint=> 'xl', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<%= item.input :xxl, :input_html => { :class => 'sizes', :value => '0', :onChange=>"itemcalculate()", :id => 'invoice-xxl' }, :hint=> 'xxl', :label => false, :wrapper_html => { :class => 'size-wrapper' } %>
<label>Extra Notes</label>
<%= item.text_area :extra_notes %>
<!-- start image upload -->
<%= item.fields_for :images, :html => {:multipart => true} do |image| %>
<%= f.file_field :image %>
<%= image.link_to_remove "Remove Image", :id => 'remove-image' %>
<% end %>
<%= item.link_to_add "<img src='/images/icon-camera.png' id='camera-icon'/> Add an image".html_safe, :images, :id=> 'add-image' %>
<!-- end image upload -->
<div class='clear'></div>
<%= item.link_to_remove "Remove Item" %>
</div>
<% end %>
<p id='add-new-item'><%= f.link_to_add "+ Add an Item", :lineitems %></p>
</div>
<%= f.hidden_field :user_id, :value => current_user.id %>
<div class="actions">
<%= f.button :submit %>
</div>
<% end %>
I'm getting the error WARNING: Can't mass-assign protected attributes: image
WARNING: Can't mass-assign protected attributes: images_attributes
My Image model looks like this:
class Image < ActiveRecord::Base
attr_accessible :lineitem_id, :image
belongs_to :lineitem, :polymorphic => true
mount_uploader :image, ImageUploader
end
# == Schema Information
#
# Table name: images
#
# id :integer not null, primary key
# lineitem_id :integer
# image :string(255)
# created_at :datetime
# updated_at :datetime
#
Any idea what's wrong?
in your image upload section, i think you want
<%= image.file_field :image %>
instead of
<%= f.file_field :image %>
sounds/looks like you're trying to tack the image file field onto the orders form when it should be part of the nested form you created for the image.
I guess this line:
attr_accessible :lineitem_id, :image
prevents your image from saving. You should fix/improve this definition, so it accepts other parameters as well, when mass assigning.
use something like this, since im not using active record some stuff its missing.. but you should be able to adapt it to your needs..
class LineItem
attr_accessible :images_attributes
has_many :images
accepts_nested_attributes_for :images, :allow_destroy => true
end
class Image
attr_accessible :lineitem_id, :image
belongs_to :lineitem, :polymorphic => true
mount_uploader :image, ImageUploader
end
that should work
You need add :remove_image to attr_accessible.