how to execute function after cocoon add the record? - ruby-on-rails

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

Related

Add $ next to price input inputted by user

I have a recipe website made with rails and some haml on c9.io. On the show page, I'd like to display the price that the user inputted in a simple form with a dollar sign next to it. I tried using a <p> tag, however the dollar sign appears on another line.
Here is my show.html.haml file:
#post_show
%h1= #post.title
%p.username
Shared by
= #post.user.name
about
= time_ago_in_words(#post.created_at)
.clearfix
.post_image_description
= image_tag #post.image.url(:medium)
.description= simple_format(#post.description)
%h6 Notes:
.notes= simple_format(#post.notes)
%h6 Price:
%p
.price= (#post.price)
.post_data
= link_to "Visit Link", #post.link, class: "button", target: "_blank"
= link_to like_post_path(#post), method: :get, class: "data" do
%i.fa.fa-thumbs-o-up
= pluralize(#post.get_upvotes.size, "Like")
= link_to dislike_post_path(#post), method: :get, class: "data" do
%i.fa.fa-thumbs-o-down
= pluralize(#post.get_downvotes.size, "Dislike")
%p.data
%i.fa.fa-comments-o
= pluralize(#post.comments.count, "Comment")
- if #post.user == current_user
= link_to "Edit", edit_post_path(#post), class: "data"
= link_to "Delete", post_path(#post), method: :delete, data: { confirm: "Are you sure?" }, class: "data"
#random_post
%h3 Check this out
.post
.post_image
= link_to (image_tag #random_post.image.url(:small)), post_path(#random_post)
.post_content
.title
%h2= link_to #random_post.title, post_path(#random_post)
.data.clearfix
%p.username
Share by
= #random_post.user.name
%p.buttons
%span
%i.fa.fa-comments-o
= #random_post.comments.count
%span
%i.fa.fa-thumbs-o-up
= #random_post.get_likes.size
#comments
%h2.comment_count= pluralize(#post.comments.count, "Comment")
- #comments.each do |comment|
.comment
%p.username= comment.user.name
%p.content= comment.content
%h2 Share your opinion:
= render "comments/form"
And here is my posts' form.html.haml:
%div.container
= simple_form_for #post do |f|
= f.input :image
= f.input :title
= f.input :link
= f.input :description
= f.input :notes
= f.input :price
%br
= f.button :submit, class: "btn btn-info"
Help would be greatly appreciated.
EDIT:
So now, I have added the following:
%span .input-group-addon $ .price= (#post.price)
However, the dollar sign is on the top line, and the price is on the bottom.
p is block content so will cause a new line by default in html. You can either handle this with CSS or use something that is inline like a span tag
Haml input with preceding dollar sign just include in your code as you want to:
Or just use classes:
.input-group
%span.input-group-addon $
%input.form-control{:placeholder => "Price", :type => "text"}/
Looks like: http://jsfiddle.net/s6Xu6/1/

Rails nested form display fields in index page along with parent form

index.html.haml
I have a customer details form which has a nested form named 'goods address'. I want to display all the customer details in index page.
%tbody
- #customer_details.each do |customer|
%tr
%td= customer.customer_name
%td= customer.customer_id
%td= customer.address
%td= customer.state
%td= customer.email
nested form
= fields_for customer_detail do |f|
= f.fields_for :goods_address do |t|
.fieldset
.row
.col-sm-12
= t.label :delivery_address,"Goods to be delivered at same address mention above?", class: "col-sm-6 control-label text-right"
= t.check_box :delivery_address, autofocus: true, class: "toggle-two customer-goods col-sm-6", :value => "On"
%br
%div{id: 'customer_goods'}
.main-center-pan.main-login-pan
.fieldset
.row
.col-sm-12
= t.label :name,"Name", class: "col-sm-3 control-label text-right"
= t.text_field :name, autofocus: true, class: "col-sm-3"
= t.label :contact_no,"Contact number", class: "col-sm-3 control-label text-right"
= t.text_field :contact_no, autofocus: true, class: "col-sm-3"
%br
parent form
= nested_form_for(#customer_detail) do |f|
- all_views_side_error_messages!(#customer_detail)
.form-alignment
.fieldset
.row
.col-sm-12
= f.label :customer_name,"Organisation/Customer Name", class: "col-sm-3 control-label text-right"
= f.text_field :customer_name, autofocus: true, class: "col-sm-3"
= f.label :residential_type,"Residential Type", class: "col-sm-3 control-label text-right"
= f.check_box :residential_type, autofocus: true, "data-width" => "200", class: "toggle-two-resident select-resident col-sm-3"
%br
The problem is I dont know how to define the nested form's fields in the index.can someone plz help me.Thanks in advance.
If you want to display the child-attributes(goods_address) with parent(customer_details), just loop the association of the parent object.
%tbody
- #customer_details.each do |customer|
%tr
%td= customer.customer_name
%td= customer.customer_id
%td= customer.address
%td= customer.state
%td= customer.email
- if customer.goods_address.present?
%td= customer.try(:goods_address).try(:delivery_address)
%td= customer.try(:goods_address).try(:name)
%td= customer.try(:goods_address).try(:contact_no)

Rails, MongoMapper nested Array in HAML form trouble

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'

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.

I am having difficulties with creating an equipment

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)

Resources