I have this simple_form
<%= simple_form_for(#order) do |f| %>
<%= f.error_notification %>
<%= f.association :orderstatus, :label => false, :include_blank => false, :input_html => { :class => 'order-status' } , :as => :radio, :label_html => { :style => "background-color:black;" } %>
<%= f.button :submit, :value => 'Update', :class => 'button grey small' %>
<% end %>
And it creates this: http://d.pr/9Bqd In the database I also have a field color which is a hex code for the background color I want of each status. Any idea how to pass this hex code onto each background label color?! I've tried for hours.
Have you tried passing it in like this...
# assuming "hex" is stored in the order model. Any variable should work
:label_html => { :style => "background-color:##{#order.hex};" }
If not, please respond with your results
Related
I have two forms, one containing a dropdown where the user can choose how a list is beeing sorted, the other one containing a searchfield, where the user can search through that list. Now if a user searches for "test" and ten results show up, I want the user to be able to choose from the dropdown, how the results are beeing sorted. Accordingly if he sorts the whole list, I want himto be able to search through the list, with the results showing up in the sorted way he choose before. Due to code restrictions I have to keep those two inputs in different forms.
Here is the sort-dropdown:
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
And here is the search:
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
Is it possible to keep the two inputs seperate or would it be way easier to use just one form?
You can use separate forms if you need to, just store the other parameter in a hidden field in each of the forms.
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
= f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
= f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
I have a form that updates an #identity object. Two #identity attributes need to be assigned from a separate collection #accounts.
#accounts = [{
'name' => 'A',
'page_id' => 1},
{
'name' => 'B',
'page_id' => 2
}]
The form needs display the name, but pass both the name and page_id.
<%= simple_form_for(#identity, :remote => true) do |f| %>
<%= f.input :page_name, :collection => #accounts.map { |a| a['name'] }, :as => :radio_buttons, :item_wrapper_tag => :li %>
<%= f.submit 'Submit', :class => 'btn btn-primary' %>
<% end %>
How can I also pass the matching page_id attribute without displaying it?
:collection => #accounts.map { |e| [e['name'], e['page_id']] }
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.
Here is my view:
<%= form_tag({ :action => "display"}, :method => "get") do %>
<%= select(:music, :type, MusType::TYPES, {:include_blank => true}) %>
Here is my array constant in the model:
class MusType < ActiveRecord::Base
TYPES = ['Jazz','Rock','Blues']
end
My select menu draws values out of an array. How do I pass the selected value into the controller as a parameter after the submit button is pressed?
you can do something like below.
view code
<%= form_tag(:url => {:controller => "mycontroller" :action => "display"}, :method => "get") do %>
<%= select_tag(:music, :type, MusType::TYPES, {:include_blank => true}) %>
<%= submit_tag "Search", :id => 'search' %>
<% end %>
read the selected value in mycontroller.rb
def display
value = params[:music][:type]
// do something with value
end
This is my form partial:
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
If the action is edit I want to show this instead:
<%= f.simple_fields_for :photo, :html => { :multipart => true } do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
How can i achieve this?
current_page?(action: 'edit')
See ActionView::Helpers::UrlHelper#current_page?
Rails also makes the methods controller_path, controller_name, action_name available for use in the view.
Generally the form partial only contains the fields, not the form tag or the fields for, but if you have no other way, you can always see what params[:action] is currently set to and behave accordingly.
You could write something like
<%- form_url = #object.new_record? ? :photo_attributes : :photo %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
That is, if you have an #object to check against. Otherwise you could use action_name (and even controller_name).
So something like:
<%- form_url = action_name == :edit ? :photo : :photo_attributes %>
<% f.simple_fields_for form_url, :html => { :multipart => true } do |d| %>
Hope this helps.
Rails 5: Display Action within the view
<%= action_name %>
If statement within the view
<% if action_name == "edit" %>
This is an edit action.
<% end %>
Just use #_controller.action_name in view