Suddenly my Rails application acts weirdly. If I attach an image on a edit page and submit the form, show action is called instead of update action. If I don't attach any image, it's ok.
Routes
namespace :admin do
resources :products do
collection do
get :download_images
get :all_styles
post :compress_images
end
end
end
View
%h1 Edit Product
= form_for [:admin, #product], :html => {:multipart => true} do |f|
- if f.object.accessory?
= render 'form_accessory', :f => f
- else
= render 'form', :f => f
[_form partial]
:javascript
function exclusiveCheck(check_box) {
if (check_box.checked) {
$('#pictures_fieldset .is_main').attr('checked', false);
check_box.checked = true;
}
}
- error_messages_for f.object
%fieldset.span-23
%legend
.row
= f.label :category_id, :class => "required"
%br
= f.select :category_id, array_of_categories('garment'), :include_blank => true
= f.check_box :new_in_category
= image_tag "new_icon.png"
.row
= f.label :style_no, :class => "required"
%br
= f.text_field :style_no, :style => "width: 5em;"
.row
= f.label :name, :class => "required"
%br
= f.text_field :name
.row
= f.label :fabric_info, :class => ""
%br
= f.text_field :fabric_info, :style => "width: 90%;"
.row
= f.label :lb_per_piece, "Weight", :class => "required"
%br
= f.text_field :lb_per_piece, :style => "width: 3em;"
.row
.span-3
= f.label :price, :class => "required"
%br
= f.text_field :price, :style => "width: 6em;"
.span-3
= f.label :original_price, :class => ""
%br
= f.text_field :original_price, :style => "width: 6em;"
.span-10
%label Discount Rate
%br
= text_field_tag :percent, nil, :style => "width: 3em;", :id => "percent"
= "%"
= link_to_function "Set", "setPrice();"
= f.check_box :new_in_sale
= image_tag "new_icon.png"
.row
= f.label :video, :class => ""
%br
= f.text_field :video, :style => "width: 15em;"
(Upload videos to data.ohyesfashion.com at ~/files/videos directory.)
.row
= f.label :available_on
%br
= f.text_field :available_on, :class => 'date'
.row
= f.check_box :active
= f.label :active, :class => ""
.row
= f.check_box :new_arrival
= f.label :new_arrival, :class => ""
= f.check_box :new_in_new_arrival?
= image_tag "new_icon.png"
.row
= f.check_box :best
= f.label :best, :class => ""
= f.check_box :new_in_best
= image_tag "new_icon.png"
.span-24.last
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
%fieldset
%legend Sizes
- Product::SIZES.each do |size|
.span-2
= size.to_s.upcase
= f.select size, [*0..20].map { |i| i.to_s }
.span-24
.span-12
%fieldset
%legend Descriptions
- Description.order("code, name").each do |description|
.row
= check_box_tag "descriptions[]", description.id, f.object.descriptions.include?(description)
= "[#{description.code}]"
= description.name
.row
New Description
= text_field_tag :new_description_name
.span-12.last
%fieldset(style="float: right;")
%legend Colors
.span-11
- Color.order("name").each do |color|
.row
.span-1
= check_box_tag "colors[]", color.id, f.object.colors.include?(color)
.span-4
= color_box color
.span-3
= check_box_tag "sold_out_colors[]", color.id, (f.object.colors.include?(color) and Colorship.sold_out?(f.object, color))
Sold Out
.span-2
= check_box_tag "is_new_colors[]", color.id, (f.object.colors.include?(color) and Colorship.is_new?(f.object, color))
New
.span-24.last
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
- (15 - f.object.pictures.size).times { f.object.pictures.build }
%fieldset#pictures_fieldset
%legend Images
.row
= f.label :model_id, "Model"
= f.select :model_id, PhotoModel.all.map { |i| [i.name, i.id] }, :include_blank => true
%br
= f.fields_for :pictures do |ff|
.span-7(style="height: 150px;")
- if ff.object.new_record?
= ff.file_field :image
- else
= image_tag ff.object.image.url(:w45)
%br
= ff.object.image_file_name
%br
= ff.check_box :is_main, :onclick => "exclusiveCheck(this)", :class => "is_main"
Title
%br
= ff.check_box "_destroy"
Delete
.span-24.last
= link_to icon_for(:back, "Back"), :back, :class => "button negative"
%button.button.positive(type="submit")
= icon_for(:accept, "Save")
:javascript
function setPrice() {
if ($('#product_original_price').val() != '' && $('#percent').val() != '') {
var originalPrice = parseFloat($('#product_original_price').val());
var percent = parseFloat($('#percent').val());
$('#product_price').val(originalPrice * (100 - percent) / 100.0);
}
}
Any idea what's going on? It used to work fine.
Related
I'm upgrading rails version to rails6 then the error below occurred.
I don't know where to fix it.
Could anyone have any ideas please share with me...
ActionView::Template::Error (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "company_groups"
LINE 1: SELECT "companies".* FROM "companies" WHERE (company_groups....
^
4: .control-group
5: = f.label :company_id, :class => 'control-label'
6: .controls
7: = f.collection_select :company_id, Company.user_companies, :id, :name
8: .control-group
9: = f.label :service_unit_id, :class => 'control-label'
10: .controls
app/views/admin/service_contracts/_form.html.haml:7
app/views/admin/service_contracts/_form.html.haml:2
app/views/admin/service_contracts/edit.html.haml:4
this is _form.html.haml
- action = (action_name == "new") ? "create" : "update"
= form_for #service_contract, url: {action: action},
:html => { :class => 'form-horizontal' } do |f|
.control-group
= f.label :company_id, :class => 'control-label'
.controls
= f.collection_select :company_id, Company.user_companies, :id, :name
.control-group
= f.label :service_unit_id, :class => 'control-label'
.controls
= f.collection_select :service_unit_id, ServiceUnit.all, :id, :name
.control-group
= f.label :active, :class => 'control-label'
.controls
= f.check_box :active, :class => 'check_box'
.control-group
= f.label :in_trial, :class => 'control-label'
.controls
= f.check_box :in_trial, :class => 'check_box'
.control-group
= f.label :price, :class => 'control-label'
.controls
= f.number_field :price, :class => 'number_field'
.control-group
= f.label :unlocked_on, :class => 'control-label'
.controls
= f.text_field :unlocked_on, type: 'date'
.control-group
= f.label :dealer_id, :class => 'control-label'
.controls
= f.collection_select :dealer_id, Company.dealers, :id, :name
.control-group
= f.label :description, :class => 'control-label'
.controls
= f.text_area :description, :class => 'text_area', rows: 4
.form-actions
= f.submit nil, :class => 'btn btn-primary'
= link_to t('.cancel', :default => t("helpers.links.cancel")),
admin_service_contracts_path, :class => 'btn'
I fixed another part witch had same error showed the problem clearly so I could figure it out like this below
Book.includes(calendar: :company).where("companies.id = ?", company).count
fix it to this↓↓↓↓↓↓↓↓↓↓
Book.includes(calendar: :company).where("companies.id = ?", company).references(:companies).count
thank you.
form_for :radio, url(:radio, :add), :method => :put do |f|
= f.error_messages
%p
= f.label :title, :caption => "Radio Name:"
= f.text_field :title
%p
= f.select :city_name, XXXXXXXXXXXXXX
%p
= f.label :frequency
= f.text_field :frequency
%p
= f.label :url
= f.text_field :url
%p
= f.submit "Add", :class => 'button'
When I put XXXXXXXX as
[['Hot','hot'],['Medium','medium'],['Cold','cold']]
or even
City.all.map {|c| [c.city]}
I get:
can't convert Array into Hash
Any help will be appreciated.
= f.select :city_name, City.all.map{ |c| [c.name, c.id] } try this
You just need to wrap the options in options_for_select. For example:
= f.select :city_name, options_for_select([['Hot','hot'],['Medium','medium'],['Cold','cold']])
data = %w/hot medium cold/.map { |s| [s.capitalize, s] }
= f.select :city_name, Hash[data]
Hash[data]
Please find below what solved for me
= f.select :city_name, :options => [["Delhi", "Delhi"], ["Mumbai", "Mumbai"], ["New York", "New York"], ["Punjab", "Punjab"]]
:options => was the key... But how come docs does not mention this. May be this is because of gem version difference.
Thanks everyone for help.
So, I have a model (in a rails project) which has a logo and a cover image, I want to use one form for uploading both of them, is this possible to do with transloadit? I can't find an example anywhere...
this is from my project. I use it 6 times. I hope it helps..
new.html.haml
- title t('.new_provider')
.spacer
.row
.span12
= render 'shared/flash_messages'
%h1= yield :title
.row
.span12
= bootstrap_form_for #provider, validate: true, html: {edit: "false", id: "providerform", enctype:"multipart/form-data", class: 'form-horizontal' } do |f|
= transloadit_jquerify :providerform, :wait => true, :modal=>false
= f.error_messages
= render 'form', f: f
.form-actions
= f.submit t('.create'), id: 'provider_submit', class: 'btn btn-large btn-primary', disable_with: t('helpers.disable_with')
= render 'providers/modal_phone_verification'
_form.html.haml
= f.text_area :business_description, :id =>"business_description", :rows => "4"
.controls
%p.help-block{style:"margin-bottom: 15px; margin-top: -20px;"}
%small
= t('business_description_placeholder')
.control-group
= label_tag :business_photos, t('.business_photos'), class: 'control-label'
.controls
= render partial: 'file_upload', :locals => {f: f}
%span#file-error.help-inline{style: "display:none;color: #b44337"}=t('.file_presence_error')
.control-group
.controls
%p.help-block{style:"margin-bottom: 0px; margin-top: -20px;"}
%small
= t('image_info')
_file_uplaod.html.haml
= transloadit :image_resize
-6.times do |index|
.fileupload.fileupload-new.pull-left{style: "padding-right: 5px;", "data-provides" => "fileupload" }
.fileupload-new.thumbnail{:style => "width: 130px; height: 130px;"}
-if #provider.provider_images.present?
- if #provider.provider_images["provider_image#{index+1}"].present?
%img{:id=>"providerimage1", :src => #provider.provider_images["provider_image#{index+1}"][0]}/
.fileupload-preview.fileupload-exists.thumbnail{:style => "width: 130px; height: 130px;"}
%div
%span.btn.btn-file
%span.fileupload-new{:id => "selectbutton"}
-if index == 0
=t('select_profile_image')
-else
=t('select_image')
%span.fileupload-exists
-if index == 0
=t('select_profile_image')
-else
=t('select_image')
=file_field_tag "provider_image#{index+1}", accept: 'image/png,image/bmp,image/jpeg,image/tif,image/jpg', :onchange =>"validateImageFiles(this);", :data => {:max_file_size => 5.megabytes}
I have a Rails app and am using haml and bootstrap. One my form I have two fields for entering text, and only one is displayed, depending on a variable I set in the controller. If there is a text area I would like to size it, and the :rows attribute works but not columns.
I have also tried to use %div :class => 'span6' to widen the text_area, but it doesn't seem to work.
= form_for [#lesson_layout, #layout_field] do |f|
.field
= f.label :field_name
= f.text_field :field_name
- case #layout_type
- when "Text Field"
.field
= f.label :field_value
= f.text_field :field_value
- when "Text Area"
.field
= f.label :field_value_long
= f.text_area :field_value_long, :rows => 5, :placeholder => 'Enter text.'
.actions
= f.submit
EDIT
Tried the following code, and it did not change the size of the box.
.field
= f.label :field_value_long
= f.text_area :field_value_long, :rows => 5, :placeholder => 'Enter text using markdown.', :html => { :style => "width:300em" }
Something like this should work :
= f.text_area :field_value_long, :rows => 5, :class => "span6", :placeholder => 'Enter text.'
f.text_area(
:field_value_long,
:rows => 5,
:placeholder => 'Enter text.',
:html => { :style => "width:20em !important" }
)
OR
f.text_area(
:field_value_long,
:rows => 5,
:placeholder => 'Enter text.',
:html => { :class => "my_wide_class" }
)
I'm getting
undefined method `Carrots' for # (referencing ln#18)
When trying to edit with the below form:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- #harvest.harvested_crops.each do |harvested_crop|
= f.label :harvested_crop['crop']
= f.select harvested_crop['crop'], Crop.all.collect {|p| [ p.name, p.id ] }, {:include_blank => ''}
= f.label :harvested_crop['amount']
= f.text_field harvested_crop['amount']
%br
.actions
= f.submit 'Save'
Using the data below:
{ "_id" : ObjectId("5067846f37bca62bccc3729e"), "user_id" : "5067844637bca62bccc3729c", "photo" : "carrotsnspuds.jpg", "harvested_crops" : [ { "crop" : "Carrots", "amount" : 1112.15 }, { "crop" : "Potatoes", "amount" : 3212.44 } ] }
I've tried related Stack Overflow questions for MongoMapper, Rails and Embedded documents but I am not having any luck, perhaps due to this being a nested Array rather than EmbeddedDocument. I'm not using Formtastic or anything yet, would just like to understand the syntax required here first.
This is definitely not efficient, but this allowed me to get the job done:
= form_for #harvest do |f|
- if #harvest.errors.any?
#error_explanation
%h2= "#{pluralize(#harvest.errors.count, "error")} prohibited this harvest from being saved:"
%ul
- #harvest.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :created_at
= f.text_field :created_at, :disabled => true
%br
= f.label :photo
= f.text_field :photo
%h2 Crops
- x = 0
- #harvest.harvested_crops.each do |harvested_crop|
= f.fields_for "harvested_crops[]", harvested_crop do |hc|
%b Harvested Crop
%select{:name => "harvest[harvested_crops][" + x.to_s + "][crop]"}
- Crop.all.collect.each do |crop_name|
- if harvested_crop['crop'] == crop_name[:name]
%option{:selected => "selected", :value => crop_name[:name]}
= crop_name[:name]
- else
%option{:value => crop_name[:name]}
= crop_name[:name]
%b Amount
%input{:name => "harvest[harvested_crops][" + x.to_s + "][amount]", :value => harvested_crop['amount']}/
%br
- x += 1
%br
.actions
= f.submit 'Save'