Haml drop-down menu issue with Rails - ruby-on-rails

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.

Related

ActionView::Template::Error (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "company_groups"

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.

cocoon rails, how to get atached file name

Im using cocoon to attach files. I need to be able to remove file when editing a question. Im stuck on getting the exact file name to remove when rendering edit
div.edit_question
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
= f.fields_for :attachments do |f|
.nested-fields
= link_to_remove_association "remove #{ NAME HERE }", f
br
= f.submit 'Update'
Case is closed ))
=form_for #question, remote: true do |f|
= f.label :title, class: 'label_hidden'
= f.text_field :title
br
= f.label :body, class: 'label_hidden'
= f.text_area :body
br
- #question.attachments.each do |att|
= f.fields_for att do |f|
.nested-fields
= link_to_remove_association "remove #{ att.file.filename }", f
br
= f.submit 'Update'

Rails edit serialized JSON data

I have a column that stores JSON data. I don't know how to show it when it is on Edit state.
serialize :value, JSON
= f.fields_for :value do |ff|
.form-group
= ff.label :short
= ff.text_field :short, class: 'form-control'
.form-group
= ff.label :long
= ff.text_field :long, class: 'form-control'
In place of
= f.fields_for :value do |ff|
please use the following code:
= f.fields_for :value, OpenStruct.new(#object.value) do |ff|
You will need to replace #object with your model object.

how to show simple_form vertically

= simple_form_for #session, :as => 'session', :url => session_path do |form|
%form
.form-group
.form
= form.input :email
.form
= form.input :password
.form-actions
%button.btn.btn-primary{type: "submit"} Log in
above sample generates the following horizontal view, where input fields are to the right of labels
*Email [input]
*Password [input]
Is it possible to show them vertically on top of each other, so labels are above input fields? How can it be achieved?
*Email
[input]
*Password
[input]
= simple_form_for #session, :as => 'session', :url => session_path do |form|
%form
.form-group
.form
%div
= form.label :email
= form.email_field :email
%div
= form.label :password
= form.password_field :password
According to the github page:
https://github.com/plataformatec/simple_form
<%= f.input :username, label: 'Your username please' %>
Will make the label a block element, instead of inline_label.
Or you can specify the label and the input field seperately:
<%= f.label :username %>
<br/>
<%= f.input_field :username %>
Try this one:
%label Email*
%div
= form.input :email
%label Password*
%div
= form.input :password

Rails routing acts weirdly

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.

Resources