I am creating an equipment and I have two actions in my equipment controller, new and create.. and I have two views which is new and a partial form..
I also have four tables.. which are equipments, equipment_types, equipment_locations, and equipment_management_informations..
The form looks like..
>= error_messages_for 'equipment'
%table.contentText{:style => "width: 100%"}
- if #types
%tr
%td Equipment Type
%td= collection_select 'equipment', 'equipment_type_id', #types, :id, :name, {}, :class => "dropdownSelect"
%tr
%td Location
%td= select 'equipment', 'equipment_location_id', #equipment_locations.collect { |e| ["#{e.name.capitalize!}", e.id]},{}, :class => "dropdownSelect"
%tr
%td Serial Number
%td= text_field 'equipment', 'serial_number', :class => 'textFields'
%tr
%td MAC Address
%td= text_field 'equipment', 'mac_address', :class => 'textFields'
%tr
%td IP Address
%td= text_field 'equipment', 'ip_address', :class => 'textFields'
- if #stands
%tr
%td Stand
%td= collection_select 'equipment', 'stand_id', #stands, :id, :street_no, :include_blank => true
%tr
%td{:valign => 'top'} Description
%td= text_area 'equipment', 'description', :cols => 45, :rows => '3', :class => "txt_area_effect"
and my new view is like:
> %h3 New equipment
%div{:style => "border: 1px solid #CCC;"}
- form_tag :action => 'create', :estate_id => #estate do
= render :partial => 'form'
= submit_tag "Create"
- action = "list"
%input{:type => "button", :value => "Back", :onclick => "window.location='#{action}';", :class => "start"}
and my create action display this a nil when i inspect it
#equipment = Equipment.find(params[:equipment])
it gives an error like
Unknown key(s): equipment_type_id, mac_address, description, equipment_location_id, serial_number, ip_address
#equipment = Equipment.find(params[:equipment])
should be:
#equipment = Equipment.new(params[:equipment])
or
#equipment = Equipment.new(params[:equipment])
(just posting answer from comments so this stops showing as unanswered)
Related
I'm using the cocoon gem in my rails project, and I have two "link_to_add_association", but I need that depending on which link_to is pressed to do a certain function, link_to with id "link_1" adds the records to the beginning of the table and the other one adds the records at the end with other values, this is my partial form:
= form_for([#billing, #payment]) do |f|
- if #payment.errors.any?
= render partial: 'layouts/partials/errors/form_errors', locals: { errors: #payment.errors }
= f.hidden_field :billing_id, value: #billing.id
.row
.col-md-6
.form-group.row
.col-md-4
= f.label :payment, class: 'form-control-label'
.col-md-8
= f.text_field :payment, class: 'form-control text-right', 'data-currency-format--live': '', 'data-target': '#total-pay'
.col-md-6
.form-group.row
.col-md-4
= f.label :day, class: 'form-control-label'
.col-md-6
= f.text_field :day, class: 'form-control datepicker', 'data-startView': 'days', 'data-minyear': #billing.date_bill.year, 'data-minmonth': #billing.date_bill.month, 'data-minday': #billing.date_bill.day
.col-md-1
.links
= link_to_add_association 'add', f, :payment_details, partial: 'payment_detail_fields',:"data-association-insertion-node" => "tbody.details",:"data-association-insertion-method" => "prepend", class:"btn btn-success btn-xs", id: "link_1"
.col-md-1
.links
= link_to_add_association 'add', f, :payment_details, partial: 'payment_detail_discount_fields',:"data-association-insertion-node" => "tbody.details",:"data-association-insertion-method" => "append", class:"btn btn-success btn-xs", id: "link_2"
.col-md-12
#payment_details
%table.tabla_personalizada
%thead
%tr
%th value_paid
%th voucher
%th accion
%th value_paid
%th value_paid
%th voucher
%th accion
%tbody.details
= f.fields_for :payment_details do |payment_detail|
= render 'payment_detail_fields', f: payment_detail
%tr
%td
%td
%td
%td
%td
%td
%td
%tr.descuentos
%td Descuento
%td Descuento
%td
%td
%td
%td
%td
.row
.col-md-12
= f.label :comments, class: 'form-control-label'
= f.text_area :comments, class: 'form-control', rows: 6
%hr
.row
.col-md-8
.form-group.row
.col-md-8
= f.submit :class => 'btn btn-primary'
- content_for :javascript do
:javascript
$(document).ready(function() {
$("#link_1").on("cocoon:after-insert", function() {
alert("element of link_1");
} )
$("#link_2").on("cocoon:after-insert", function() {
alert("element of link_2");
} )
});
I tried this code but I can't find a way to make it work
$(document).ready(function() {
$("#link_1").on("cocoon:after-insert", function() {
alert("element of link_1");
} )
$("#link_2").on("cocoon:after-insert", function() {
alert("element of link_1");
} )
});
I have a page with two forms. It's a nic setting form. I have given everything a css id just to make them easily selectable.
%form#mnic_form
.datagrid
%table
%thead
%tr
%th{:colspan => "2"}
= "(eth0 - Management) Port Settings (#{#a.get_management_nic["mac"]})"
%tbody#managementnic
%tr
%td
DHCP:
%td
%input#dhcpon1{:type => "radio", :name => "dhcp", :value => "true", :checked => #a.get_management_nic["dhcp"], :onclick => "dhcpHandler('management')"} Enabled
%br
%input#dhcpoff1{:type => "radio", :name => "dhcp", :value => "false", :checked => !#a.get_management_nic["dhcp"], :onclick => "dhcpHandler('management')"} Disabled
%tr
%td
IP Address:
%td
%input#ip1{:type => "text", :name => "ip", :value => #a.get_management_nic["ip"], :onblur => "IPHandler('#ip1')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
Subnet Mask:
%td
%input#subnet1{:type => "text", :name => "subnet", :value => #a.get_management_nic["subnet"], :onblur => "IPHandler('#subnet1')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
Gateway:
%td
%input#gateway1{:type => "text", :name => "gateway", :value => #a.get_management_nic["gateway"], :onblur => "IPHandler('#gateway1')" }
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
DNS Servers:
%td
%input#dyndns1{:type => "checkbox", :name => "dynamicDNS", :value => "true", :checked => #a.get_management_nic["dynamicDNS"], :onclick => "dynamicDNSHandler('management')"}
Automatically assign DNS from DHCP Server
%tr
%td
DNS 1:
%td
%input#dns11{:type => "text", :name => "dns1", :value => #a.get_management_nic["dns1"], :onblur => "IPHandler('#dns11')" }
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
DNS 2:
%td
%input#dns21{:type => "text", :name => "dns2", :value => #a.get_management_nic["dns2"], :onblur => "IPHandler('#dns21')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%input#mnic_submit{:type => "button", :value => "Save", :onclick => "submitMNic()"}
%form#vnic_form
.datagrid
%table
%thead
%tr
%th{:colspan => "2"}
= "(eth1 - Video) NIC Port Settings (#{#a.get_video_nic["mac"]})"
%tbody#videonic
%tr
%td
DHCP:
%td
%input#dhcpon2{:type => "radio", :name => "dhcp", :value => "true", :checked => #a.get_video_nic["dhcp"], :onclick => "dhcpHandler('video')"} Enabled
%br
%input#dhcpoff2{:type => "radio", :name => "dhcp", :value => "false", :checked => !#a.get_video_nic["dhcp"], :onclick => "dhcpHandler('video')"} Disabled
%tr
%td
IP Address:
%td
%input#ip2{:type => "text", :name => "ip", :value => #a.get_video_nic["ip"], :onblur => "IPHandler('#ip2')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
Subnet Mask:
%td
%input#subnet2{:type => "text", :name => "subnet", :value => #a.get_video_nic["subnet"], :onblur => "IPHandler('#subnet2')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
Gateway:
%td
%input#gateway2{:type => "text", :name => "gateway", :value => #a.get_video_nic["gateway"], :onblur => "IPHandler('#gateway2')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
DNS Servers:
%td
%input#dyndns2{:type => "checkbox", :name => "dynamicDNS", :value => "true", :checked => #a.get_video_nic["dynamicDNS"], :onclick => "dynamicDNSHandler('video')"}
Automatically assign DNS from DHCP Server
%tr
%td
DNS 1:
%td
%input#dns12{:type => "text", :name => "dns1", :value => #a.get_video_nic["dns1"], :onblur => "IPHandler('#dns12')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
DNS 2:
%td
%input#dns22{:type => "text", :name => "dns2", :value => #a.get_video_nic["dns2"], :onblur => "IPHandler('#dns22')"}
%img.spinner{:src => '/img/busy_spinner.gif', :style => "display: none", :height => "15"}
%tr
%td
%td
%p All multicast routed on this interface
%input#vnic_submit{:type => "button", :value => "Save", :onclick => "submitVNic()"}
Now when I go to test this using capybara, I try these two functions:
page.choose('dhcpoff1')
page.choose('dhcpoff2')
and the first one works perfectly, and the second one does nothing. This shouldn't be that difficult to figure out, but I'm completely stuck. Been staring at it for hours. I appreciate any insight you might have.
function dhcpHandler(m_or_v) {
var tmp = (m_or_v === "management") ? tmp = $('#managementnic > tr > td > input') : tmp = $('#videonic > tr > td > input');
if (tmp[0].checked) {
tmp[2].disabled = true;
tmp[3].disabled = true;
tmp[4].disabled = true;
}
else {
tmp[2].disabled = false;
tmp[3].disabled = false;
tmp[4].disabled = false;
}
}
More testing. There are controller methods above, so let me just say that the page starts with dhcpon2 selected, and ip2, gateway2, and subnet2 all disabled.
I'm trying to run page.choose('dhcpoff2') and get it to select dhcpoff2, and then trigger the dhcpHandler() to un-disable the 3 fields. a workaround that works is page.execute_script("$('#dhcpoff2').click()") but that's too unbearable. currently I have it failing in selenium (2.48.1, capybara 2.5.0), and working in poltergeist...??? so to be more specific, here's a test:
page.choose('dhcpoff2')
#page.execute_script("$('#dhcpoff2').click()")
assert !find('#ip2')["disabled"]
if we comment out line 2 (the ghetto work around) it passes with poltergeist as the driver, but not with selenium as the driver.
And finally, here's the driver setup I'm using, swapping which lines are commented to switch between poltergeist and selenium:
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, js_errors: false, phantomjs: "C:/Ruby22-x64/bin/phantomjs.exe")
end
Capybara.default_driver = :selenium
Capybara.javascript_driver = :selenium
#Capybara.default_driver = :poltergeist
#Capybara.javascript_driver = :poltergeist
Capybara.default_max_wait_time = 10
Capybara.ignore_hidden_elements = false
I have the following code to create a nested model (invoice_line_items) within my invoice model:
= nested_form_for #invoice, mutipart: true, class: "form-horizontal" do |f|
...
%table.table.table-bordered{:id => "line-items"}
%thead
%tr
%th
%th Description
%th Quantity
%th Rate
%tbody
%tr
= f.fields_for :invoice_line_items do |line_item|
%td= line_item.link_to_remove "X"
%td= line_item.text_field :description
%td= line_item.text_field :quantity, :class => "input-mini"
%td= line_item.text_field :rate, :class => "input-mini"
= f.link_to_add "Add", :invoice_line_items, :data => { :target => "#line-items" }
I have two problems: 1) when I add a new row by clicking "Add"....it doesn't match the table formatting and doesn't insert into the table. I tried everything to get it to work and it just doesn't. I also tried adding the ":target" as mentioned by ryanb in his gem docs. 2) I would like to have 3 invoice_line_items initially ready for the user on the invoice page, but i'm not sure how to do that.
EDIT: I got something a little different now since I've been playing around with it. I don't think i'm doing it right still but now it creates a new form each time I click "add":
.row-fluid
= f.fields_for :invoice_line_items, :wrapper => false do |line_item|
%table.table.table-bordered#tasks
%thead
%th
%th Description
%th Quantity
%th Rate
%tr.fields
%td= line_item.link_to_remove "X"
%td= line_item.text_field :description
%td= line_item.text_field :quantity, :class => "input-mini"
%td= line_item.text_field :rate, :class => "input-mini"
.row-fluid
= f.link_to_add "Add", :invoice_line_items, :data => { :target => "#tasks" }
This was a tough one to figure out but it turns out that the most updated version of the nested_form gem doesn't come with the most recent version of the jquery_nested_form.js file (even though the documentation says to use this function). So, to get this to work, I had to add this to my application.js:
$(function ($) {
window.NestedFormEvents.prototype.insertFields = function(content, assoc, link) {
var target = $(link).data('target');
if (target) {
return $(content).appendTo($(target));
} else {
return $(content).insertBefore(link);
}
};
});
this overwrote the function in jquery_nested_form.js and now everything works.
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
We use Ruby (1.9.2) Rails (2.3).
I'm trying to set pre-selection for radio buttons...
- form_for #user, :url => plan_user_url, :html => { :method => 'put', :class => 'form' } do |f|
- #plans.each do |p|
%span
%p= p[:blurb]
%p= p[:price]
- p[:features].each do |f|
%p= f
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
= f.label :plan_name, p[:name]
%p
%br
.spacer
.field.first
= f.submit 'Update', :class => 'button ok'
.field
= link_to 'Cancel', redirect_back_url || root_url, :class => 'button cancel'
HAML doesn't like this line:
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
Any help is appreciated.
This is invalid Ruby code:
= f.radio_button {:id => p[:id], :checked => #user[:plan_id]==p[:id] || nil}
You're attempting to call the radio_button method and Ruby thinks you're passing it a block, but really you're passing it a Hash. This is better:
= f.radio_button :id => p[:id], :checked => #user[:plan_id]==p[:id] || nil
That removes the ambiguity between Proc and Hash, but it's still weird. Why do you want the || nil? I think it's unnecessary:
= f.radio_button :id => p[:id], :checked => #user[:plan_id] == p[:id]
Thanks for the hints from Brian. It turns out
f.radio_button :plan_id, p[:id]
works for pre-select.