I have applied localization for a select box label as follows. Label option not working in collection_select in rails form. How can I change the code to get the label
Updated
= simple_form_for #product do |f|
= error_notification f
.form-inputs
.row
.span5
= f.input :name, :input_html => {:maxlength => 100}
.span5
- unless #product.company.nil?
= f.input :company_id, :as => :hidden, :input_html => { :value => #product.company.id}
- else
= f.association :company, :prompt => "Select Company"
.row
.span10
%div#product_existence_message
- unless #product.company.nil?
= f.collection_select :product_type_id, ProductType.all,:id, :name, {:prompt => 'Select Product Type', :label => :label => t('forms.products.label.name'), :selected =>#product.product_type_id }
File: en.yml
en:
activerecord:
attributes:
product:
product_type: 'Product Type translation here'
File: template.html.erb
<%= label(:product, product_type) %>
# => <label for="product_product_type">Product Type translation here</label>
Reference: https://apidock.com/rails/ActionView/Helpers/FormHelper/label[https://apidock.com/rails/ActionView/Helpers/FormHelper/label]
How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"
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.
I'm having a bear with some very basic code, which is ...
.form
= semantic_form_for 'thought', :url => thoughtstep2_path do |f|
= f.inputs :name => 'Add Something' do
= f.input :title, :hint => "A Hint", :input_html => { :value => params[:thought][:title] }
= f.input :moreinfo, :as => "text", :hint => "Another Hint", :input_html => {:value => params[:thought][:moreinfo]}
= f.buttons
Because the params array I am using to set the :value has a second dimension it bugs out with
You have a nil object when you didn't expect it!
I've tried all sorts of get arounds but to no avail, any ideas anyone?
A few options to try:
{ :value => params[:thought].try(:[], :title) }
{ :value => (params[:thought][:title] rescue nil) }
{ :value => params[:thought] && params[:thought][:title] }
{ :value => (params[:thought][:title] if params[:thought]) }
I'd suggest that you pick the one that you personally find the most readable.
In your helper :
def test(value)
if value
return value;
else
return '';
end
end
In your view :
.form
= semantic_form_for 'thought', :url => thoughtstep2_path do |f|
= f.inputs :name => 'Add Something' do
= f.input :title, :hint => "A Hint", :input_html => { :value => test(params[:thought][:title]) }
= f.input :moreinfo, :as => "text", :hint => "Another Hint", :input_html => {:value => params[:thought][:moreinfo]}
= f.buttons
I'm not sure about this but it's may be a way to fix your problem
I have a very long form with nested form fields. It takes 30 seconds in development to load.
I've used a separate model for each drop down menu that I need - I don't know if that's such a good idea now that I have 35 of them. I also have 11 accepts_nested_attributes_for/ cocoon gem partials, which is super once the page has loaded, but I suspect the 11 different partials are slowing things down.
I'm caching the page which helps, and in production it's a lot quicker - 8 seconds - but still not really acceptable.
How should I ideally structure this long form? I can't think it's RESTful to split up an edit / update method. Or if the structure is OK, what can I do to improve performance? Like I say I'm caching, and there are no finds in the view. The controller is pretty regular although there's lots of calculations in methods in the model.
Here's a fragment. Many thanks in advance for any thoughts.
%a{:name => "Measurements"}
- f.inputs :name => "Measurements", :id => "main" do
%li.tip
= tooltip(:descriptivedetail_productcomposition_productcomposition, :hover)
= f.input :descriptivedetail_productcomposition_productcomposition, :label => "Product composition", :as => :select, :collection => Productcomp.all, :value_method => :code
%li.list
= link_to "Edit list", productcomps_path
%br
%li.tip
= tooltip(:descriptivedetail_productcomposition_productform, :hover)
= f.input :descriptivedetail_productcomposition_productform, :label => "Product form", :as => :select, :collection => Productform.all, :value_method => :code
%li.list
= link_to "Edit list", productforms_path
%br
%li.tip
= tooltip(:descriptivedetail_primarycontenttype, :hover)
= f.input :descriptivedetail_primarycontenttype, :label => "Content type", :as => :select, :collection => Contenttype.all, :value_method => :code
%li.list
= link_to "Edit list", contenttypes_path
%br
%li.tip
= link_to_add_association 'Add measurement', f, :measurements
= f.semantic_fields_for :measurements do |measurement|
= render 'measurement_fields', :f => measurement
%br
%li.tip
= link_to_add_association 'Add extent', f, :extents
= f.semantic_fields_for :extents do |extent|
= render 'extent_fields', :f => extent
%br
%a{:name => "Supply"}
- f.inputs :name => "Supply", :id => "main" do
%li.tip
= link_to_add_association 'Add supply info', f, :supplies
= f.semantic_fields_for :supplies do |supply|
= render 'supply_fields', :f => supply
%br
%a{:name => "Rights"}
- f.inputs :name => "Rights", :id => "main" do
%li.tip
= link_to_add_association 'Add rights info', f, :rights
= f.semantic_fields_for :rights do |right|
= render 'right_fields', :f => right
%br
%a{:name => "Related"}
- f.inputs :name => "Related", :id => "main" do
%li.tip
= link_to_add_association 'Add related product info', f, :relatedproducts
= f.semantic_fields_for :relatedproducts do |relatedproduct|
= render 'relatedproduct_fields', :f => relatedproduct
%br
An example partial:
.nested-fields
= f.inputs do
%br
%li.tip
= tooltip(:descriptivedetail_measure_measuretype, :hover)
= f.input :descriptivedetail_measure_measuretype, :label => "Measure type", :as => :select, :collection => Measuretype.all, :value_method => :code
%li.list
= link_to "Edit list", measuretypes_path
%br
%li.tip
= tooltip(:descriptivedetail_measure_measurement, :hover)
= f.input :descriptivedetail_measure_measurement, :label => "Measurement"
%li.tip
.links
%br
%li.tip
= tooltip(:descriptivedetail_measure_measureunitcode, :hover)
= f.input :descriptivedetail_measure_measureunitcode, :label => "Unit", :as => :select, :collection => Measureunit.all, :value_method => :code
%li.list
= link_to "Edit list", measureunits_path
%br
= link_to_remove_association "Remove measurement", f