Bootstrap modal nested model form view messed up - ruby-on-rails

Here is what happens
Here is my views code
Form
<%= simple_form_for([:supplier, #fuel_price],remote: true, :html => {:class => 'form-vertical' }) do |f| %>
<%= f.simple_fields_for :fuel_products do |fuel_products_form| %>
<div class="field">
<%= render partial: 'fuel_products_fields', locals: {f: fuel_products_form} %>
</div>
<% end %>
<%= link_to_add_fields "Add more", f, :fuel_products %>
<div class="modal-footer">
<%= f.button :submit, "Update", class: "btn btn-success"%>
</div>
<%end%>
Fuel Products Field Partial
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
Javascript code
ready = ->
$('#FuelmodelBody').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('.field').hide()
event.preventDefault()
$('#FuelmodelBody').on 'click', '.add_fields', (event) ->
console.log('It is really happening ....')
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(document).ready(ready)
$(document).on('page:load', ready)
Views Modal code
<div class="modal inmodal" id="newFuelPrice" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content animated bounceInRight">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<i class="fa fa-usd modal-icon" style="color:#1ab394"></i>
<h4 class="modal-title">Add New Fuel Price</h4>
<span class="font-bold">Your contacts will get text message of your latest price</span>
</br>
<span class="font-bold">As (your fuel price + the formula).</span>
</div>
<div class="modal-body" id="FuelmodelBody">
<!-- form -->
</div>
</div>
</div>
</div>
I also have a helper function for link_to_add_fields
def link_to_add_fields(name, f, association)
# creates a new instance of the 'has_many' object
new_object = f.object.send(association).klass.new
# new_object = f.object.association.klass.new
# f.object.association.klass # => Document
# new_object = f.object.documents.build Document(user_id: f.object.id)
# gets the object id
id = new_object.object_id
# creates a new form for the association
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s + '_fields', f: builder)
end
link_to(name, '#', class: 'add_fields', data: { id: id, fields: fields.delete("\n") })
end
Moving around code since morning and still could not figure out how to fix those buttons by aligning side by add, the add more will also be button in blue, i was just playing around and converted it to link. I am Using simple Form

If you want to align your "Remove" buttons with their corresponding Fuel type and Price fields, then you need to put them all in a single Bootstrap row like so:
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<ul class="list-unstyled">
<li><%= link_to '#', class: "btn btn-danger btn-xs remove_fields" do %>
Remove
<%end%></li>
</ul>
</div>
</div>
Also, you probably don't need that extra ul tag. It does not carry any additional meaning, since you have just one button in there. It is not a list.
<div class="row">
<div class="col-md-6">
<%= f.input :fuel, label: "Fuel Type", :collection => fuel_types, class: "form-control select",:selected => "87 RFG" %>
</div>
<div class="col-md-3">
<%= f.input :price, class: "form-control", placeholder: "$1.25" %>
</div>
<div class="col-md-3">
<%= f.hidden_field :_destroy %>
<%= link_to 'Remove', '#', class: "btn btn-danger btn-xs remove_fields" %>
</div>
</div>

Related

Deep rails nested attributes on the fly

I have a form, the form has 'fields_for :addresses' and renders partial 'address_fields' in that 'fields_for :addresses', in that partial 'address_fields' there is 'fields_for :sub_address', when the browser is first loaded, the 'fields_for :sub_address' in the partial 'address_fields' appears, but when the 'Add Addresses' button is pressed the "fields_for :sub_address" element does not appear. So basically I have a nested form inside the nested form 'addresses', but it doesn't render when I click on 'Add Address'.
This is my 'Add Address' javascript code:
class AddFields {
constructor() {
this.links = document.querySelectorAll('.add_fields');
this.iterateLinks();
}
iterateLinks() {
if (this.links.length === 0) return
this.links.forEach(link => {
link.addEventListener('click', e => {
this.handleClick(link, e)
});
});
}
handleClick(link, e) {
if (!link || !e) return;
e.preventDefault();
let time = new Date().getTime();
let linkId = link.dataset.id;
let regexp = linkId ? new RegExp(linkId, 'g') : null
let newFields = regexp ? link.dataset.fields.replace(regexp, time) : null
newFields ? link.insertAdjacentHTML('beforebegin', newFields) : null
}
}
window.addEventListener('turbolinks:load', () => new AddFields());
here is my views:
new.html.erb
<%= form_with model: #data, url: address_path(token: params[:token]) do |form| %>
<div class="row">
<div class="col">
//another html here
</div>
</div>
<fieldset>
<div class="card mt-2">
<div class="card-body">
<%= form.fields_for :address do |addr| %>
<%= render 'sub_address_fields', form: addr %>
<% end %>
<%= link_to_add_fields 'Add Address', form, :addr %>
</div>
</div>
</fieldset>
<div class="form-group">
<%= form.submit :'Submit', name: 'submit', class: 'btn btn-primary mt-2' %>
</div>
<% end %>
```
partial view:
<div class="nested-fields">
<%= form.hidden_field :_destroy %>
<div class="row">
<div class="col">
//another html here
</div>
<%= form.fields_for :sub_address do |sub_addr| %>
<div class="row">
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label :id_card, class: 'id-card-label', for: 'id-card-file' %>
<%= sub_addr.file_field :attachment_id_card, id: 'id-card-file' %>
</div>
</div>
</div>
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label :self_assessment, class: 'self-assessment-label', for: 'self-assessment-file' %>
<%= sub_addr.file_field :attachment_self_assess, id: 'self-assess-file' %>
</div>
</div>
</div>
<div class="col">
<div class="form-group mt-2">
<div class="custom-file form-control">
<%= sub_addr.label 'SWAB', class: 'swab-label', for: 'swab-file' %>
<%= sub_addr.file_field :attachment_swab, id: 'swab-file' %>
</div>
</div>
</div>
</div>
<% end %>
<div class="form-group">
<%= link_to "Remove", '#', class: "remove_fields"%>
</div>
</div>
and this is the helper:
module Frontend::Data::DataHelper
def link_to_add_fields(name, form, association)
new_object = form.object.send(association).klass.new
id = new_object.object_id
fields = form.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + '_fields', form: builder)
end
link_to(name, '#', class: 'add_fields', data: {id: id, fields: fields.gsub('\n', '')})
end
end
Is javascript not able to display the form generated from the 'render' function on rails?

multiple forms in one view rails

am new to stack overflow..hoping support from all f u
i have employee form with personal details,contact details,salary details etc,arranged,but they are different controllers.i want to get all data in one form in employee view.employee contacts and other forms are not getting saved...help out...
`
<%= form_for(#employee, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |f| %>
<div class="form-group row">
<%= f.label :first_name, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= f.text_field :first_name, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :first_name) %></span>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= f.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
<div role="tabpanel" class="tab-pane in" id="nav-tabs-0-2">
<%= form_for(#employee.contact_components, :html => {class: 'form-horizontal add_allignment employee_form', role: 'form'}) do |e| %>
<div class="form-group row">
<%= e.label "P O Box", class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_field :po_box, class: 'form-control' %>
<span class="error-block"><%= validation_error(#employee, :po_box) %></span>
</div>
</div>
<div class="form-group row">
<%= e.label :permanent_address, class: 'col-sm-3 form-control-label' %>
<div class="col-sm-9">
<%= e.text_area :permanent_address, class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-0 col-sm-10">
<%= e.button "Submit", type: 'button', class: 'btn btn-primary btn-lg', onclick: 'submit_my_form(this)' %>
</div>
</div>
<% end %>
</div>
</div>`
What you want is a nested form. It allows you to update different model attributes from a single form. That doc link will walk you through each change you need to make to the model, controller and views.

Rails Radio button is only allowing me to select only first option and not the other

What would be the cause of me only being able to select the first radio button, but not the second. Here is what my form looks like.
`
<%= form_for [#item, Calendar.new] do |f| %>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :start_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.text_field :end_date, readonly: true, value: Date.today, class: "form-control datepicker" %>
</div>
</div>
</div>
<div class="form-group">
<div class="btn-group" data-toggle="buttons-radio">
<%= f.collection_radio_buttons :status, Calendar.statuses, :first, :first, checked: Calendar.statuses.first do |b|
b.radio_button + b.label {b.text.split("_").join("").capitalize}
end %>
</div>
</div>
<div class="row new-pricing">
<div class="col-md-10">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">$</span>
<%= f.text_field :item_price, class: "form-control", value: #item.item_price, required: true %>
</div>
</div>
</div>
<div class="col-md-2">
<p style="margin-top: 10px"> Per Day </p>
</div>
</div>
<div class="no-pricing hide">
</div>
<div class="form-group">
<%= f.button "Save", type: :submit, class: "btn btn-success" %>
</div>
<% end %>`
Here is what the html looks like when I click on the "Not available" button. The checked always remains on the first value input button.
The html in the inspection window.
How can I go about fixing this. Sorry if this was an easy question.

Rails 4 Two Forms, Separate Models On One Page

I have two models for communication, Post and Reply. For some reason I can't figure out, the #reply form is unresponsive. Submit does not work, and the file upload field does not work, but only on the #reply form, the #prosect and #post forms work perfectly.
<h1><strong><%= #group.name %></strong></h1>
<p><em><%= #group.description %></em><p>
<p>Operated by: <% #owner = User.find(#group.owner_id) %><%= #owner.name %></p>
<% if current_user.group_id == nil %>
<%= form_for(#prospect) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.submit "Join Group", class: "btn btn-primary" %>
<% end %>
<% end %>
<% if current_user.group_id == #group.id %>
<%= form_for(#post) do |f| %>
<%= f.hidden_field :group_id, value: #group.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= f.text_area :content, class: 'form-control', placeholder: "What ails you?" %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(0);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture0">
<%= f.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= f.submit %>
</div>
<% end %>
<% #group.posts.each do |p| %>
<div class="post">
<div class="post-meta">
<%= p.user.name %> <em><%= p.created_at.strftime('%a, %b %e, %Y %r') %></em>
</div>
<%= p.content %>
<% if p.image %>
<img src="data:image/png;base64,<%= p.image %>" class="img-responsive"/>
<% end %>
<% p.replies.each do |r| %>
<%= r.user.name %><br/><%= r.content %>
<% if r.image %>
<img src="data:image/png;base64,<%= r.image %>" class="img-responsive"/>
<% end %>
<% end %>
<a class="reply">Reply</a>
<div class="reply hidden">
<%= form_for(#reply) do |ff| %>
<%= ff.hidden_field :post_id, value: p.id %>
<%= ff.hidden_field :user_id, value: current_user.id %>
<div class="form-group col-xs-12">
<div class="row">
<div class="col-xs-11">
<%= ff.text_area :content, class: 'form-control', placeholder: "Write a reply..." %>
</div>
<div class="col-xs-1">
<button type="button" class="btn btn-success" onclick="addpic(<%= p.id %>);">Add a pic</button>
</div>
</div>
</div>
<div class="form-group hidden col-xs-12" id="picture<%= p.id %>">
<%= ff.file_field :image, class: 'form-control' %>
</div>
<div class="form-group col-xs-12 actions">
<%= ff.submit %>
</div>
<% end %>
</div>
</div>
<% end %>
<% end %>
<script>
var addpic = function(id) {
var el = '#picture' + id;
$(el).removeClass('hidden');
}
$('.reply').on('click', function(e){
e.preventDefault();
$(this).next().removeClass('hidden');
});
</script>
Figured out the issue, JS was preventing the default for all clicks inside a .reply div. Had reply used in the wrapper for the reply form, so it was preventing the default. Got it working now by changing the class of the reply link to 'addreply' and the js listener to 'addreply' as well.

Pass variable to Controller when clear button is clicked rails

<%= form_tag({controller: "cc_banker_assignments", action: "index"}, method: "get", class: "form-horizontal") do %>
<div class="col-md-4">
<span class="badge badge-error count-error"></span>
<div class="form-group">
<label for="" class="col-sm-4 control-label">Date of Assignment</label>
<div class="col-sm-7">
<%= text_field_tag "assignment_date", params[:assignment_date], class: "form-control", id: "date-range" %>
</div>
</div>
<div class="form-group" style="margin-bottom: 1.5em;">
<label for="" class="col-sm-4 control-label">Assigned To</label>
<div class="col-sm-7">
<%= select_tag "assigned_to", options_from_collection_for_select(#banker, :id, :name, params[:assigned_to]), class: 'form-control single-select', prompt: "All Bankers" %>
</div>
</div>
<div class="col-sm-8 pull-right" style="margin-top: -0.3em">
<%= submit_tag "Search", :class => "btn btn-default lender-search-btn btn-s-layout" %>
<%= button_tag "Clear", :type => 'reset', :class => "btn btn-primary btn-c-layout clear_button", :onClick => "check_for_cancel" %>
</div>
</div>
</div>
<% end %>
Ok so i have this form and i want to be able to pass a variable to my index method on my controller when i click on the button.
This is because i want my controller to know if the clear button has been clicked and the only way for me to do tht is to pass a variable to my controller index method.
my controller method
def index
#banker = Banker.where("locked_at is null and approved = true").order('name asc')
ap params
if !params[:get_status].nil?
params[:banker_status] = BankerAssignmentStatus.where("assignment_status =? ",params[:get_status]).pluck(:id)
end
search_banker_assignment params
render template: 'cc_banker_assignments/index'
end
How do i do this using rails
Are you trying to clear the form inputs by requesting index page? You can use link_to instead of button_tag to do that, or just javascript. If you really want to pass a variable to the controller, you can bind your button to a jQuery.ajax() call.

Resources