Rails 3.1 CarrierWave error - ruby-on-rails

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.

Related

How to add custom name input field in rails simple_form_for

#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

Simple_Form: Make two forms work with eachother (dropdown and search)

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')

Rails select tag in form doesn't populate with value in the edit view

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"} ) %>

setting the size of input fields

I have a Rails form for which I'm trying to change the size of the default fields using either :size or both :cols => "30", :rows => "10" as in the code below. However, the form fields are staying the default size. Is there something I'm doing wrong?
<%= d.text_field c, :class => 'random', :value => "#{c}", :size => "30", :id => 'correction_data_'"#{c.parameterize}"%>
or this way
<%= d.text_field #title, :class => 'random', :value => "#{#title}", :cols => "30", :rows => "10", :id => 'correction_data_'"#{#title.parameterize}"%>
text_field doesn't consider rows and cols attributes, but does consider size attribute. text_area considers rows, cols and size attributes.
text_field:
<%= d.text_field c, :class => 'random', :value => "#{c}", :size => 30, :id => 'correction_data_'"#{c.parameterize}" %>
text_area using rows and cols:
<%= d.text_area #title, :class => 'random', :value => "#{#title}", :cols => 30, :rows => 10, :id => 'correction_data_'"#{#title.parameterize}" %>
text_area using size:
<%= d.text_area #title, :class => 'random', :value => "#{#title}", :size => "30x10", :id => 'correction_data_'"#{#title.parameterize}" %>

Simple_form style the collection_label's

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

Resources