How to use Ajax on Rails to copy a field - ruby-on-rails

I'm trying to use ajax to copy the value from a field, after the user has selected an option.
Right now I'm not using ajax and my code is something like this:
<div class="row">
<div class="form-group col-md-5">
<%= f.label :inscription_end, "End Date" %>
<div class="input-group datetime">
<%= f.text_field :inscription_end, :value => #ofer.inscription_end.blank? ? nil : l(#ofer.inscription_end, :format => :long), :"data-date-before" => "inscription_begin", :id => "inscription_end", :title => tooltip_error_title(#ofer, :inscription_end), class: "form-control", disabled: disabled_it_result_ended %>it
<span class="input-group-addon"><%= glyph(:calendar) %></span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-5">
<%= f.label :date_documents_sending, "End date to receive documents" %>
<div class="input-group datetime">
<%= f.text_field :date_documents_sending, :value => #ofer.inscription_end %>
</div>
</div>
</div>
Is there a way to automatically send the input from :inscription_end, to :date_documents_sending as soon as the user selects the :inscription_end from the calendar?

All this does is populate the date.documents_sending with the selected value
Using Jquery "ensure your elements are correct".
$(".input-group datetime").change(function (){
var selectedDate = $(".input-group datetime option:selected").val();
$(".date_documents_sending").html(selectedDate);
)};

Related

Dynamically change radio button with select (Ruby on Rails)

I'm new at programming and Rails in particular.
I have a form to create new goal records.
I have a select to chose the goal "Horizon": "weekly", "quarterly", or "yearly"
I also have a radio button, "Due date" to set the goal as for this term or next term (e.g. in the case of a weekly goal: this week or next week).
What I would like to do: is to dynamically update the radio button, based on the selected option of the select.
<%= form_with(model: #goal, url: goals_path, id: "new-goal-form", local: true) do |f| %>
<p><%= #goal.errors[:date].first %></p>
<div class="field">
<label class="label">Title</label>
<div class="control">
<%= f.text_field :title, class: "input"%>
</div>
<p><%= #goal.errors[:title].first %></p>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<%= f.text_area :description, class: "textarea"%>
</div>
</div>
<div class="field">
<label class="label">Horizon</label>
<div class="control">
<div class="select">
<%= f.select :horizon, options_for_select({"Weekly" => "week", "Quarterly" => "quarter", "Yearly" => "year"}, #horizon), include_blank: true %>
</div>
</div>
</div>
<div class="field">
<label class="label">Due Date</label>
<div class="control">
<%= f.radio_button :date, DateTime.current.to_date.end_of_week.strftime('%Y-%m-%d'), :checked => true, id:"radio-this", class: "is-checkradio" %>
<label for="radio-this">
This <%= #horizon.capitalize %>
</label>
<%= f.radio_button :date, 1.week.from_now.to_date.end_of_week.strftime('%Y-%m-%d'), id:"radio-next", class: "is-checkradio" %>
<label for="radio-next">
Next <%= #horizon.capitalize %>
</label>
</div>
</div>
<div class="field">
<label class="label">Related goal</label>
<div class="control">
<div class="select">
<%= f.select :related_goal_id, options_from_collection_for_select(#related_goal, 'id', 'title'), include_blank: true %>
</div>
</div>
</div>
<br/>
<div class="field is-grouped">
<div class="control">
<%= f.submit "Save", class: "button is-primary" %>
</div>
<div class="control">
<%= link_to "Cancel", goals_path(horizon: #horizon), class: "button" %>
</div>
</div>
<% end %>
I have tried adding :onchange at the end of the select, but nothing actually happens when changing the selected option.
Many thanks in advance.
Are your radio buttons id'd differently? They seem to both have the same autogenerated id. That won't work in javascript. Down below you set a click event for the select box and depending on what child is selected, you set a radio button checked to true.
$(document).ready(function(){
//when select option is chosen, set radio button to selected.
$("#select").click(function(){
var option = $(this).children("option:selected").val()
if(option == "weekly"){
$("#radio_button_weekly").prop("checked", true);
}
)}
)};

How to add in multiple options for checkbox in Ruby on Rails?

This is likely a super easy answer for a Ruby on Rails expert. I have a form and need to add in a checkbox that has multiple items. I've been messing with the following code for a lot longer than I'd like to admit:
<%= form_for :lead, url: something, html: {id: 'product-form'} do |f|%>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :product, {multiple:true}, "option", "option2", :class => 'form-control'%>
</div>
</div>
</div>
<% end %>
With this code I get the error "wrong number of arguments (5 for 1..4)".
Basically I just want someone to be able to pick multiple options. I've also tried the following:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label :product%>
<%= f.check_box :option1, "Option1" :class => 'form-control'%>
<%= f.check_box :option2, "Option2", :class => 'form-control'%>
</div>
</div>
</div>
And I get the delightful "undefined method `merge' for "Option1":String". What am I missing to put the values in associated with the label?
I use Rails 5.1.6 and this is how I achieved adding multiple options for checkbox. I also placed it in a dropdown.
In the migration file:
t.string :skills
In my controller "tasks_controller.rb":
def task_params
params.require(:task).permit(:work_type, :title, :post, {skills: []})
end
In my model "task.rb" I did:
validates :skills, presence: true
serialize :skills, JSON
I created a module mobilephones_data.rb in directory "app/model/concerns/" which holds all the options of the checkbox as an array that can be edited easily.
In app/model/concerns/mobilephones_data.rb I wrote:
module Mobilenphones_Data
Activities = [
'Amazon Kindle', 'Blackberry', 'iPad', 'iPhone', 'Mobile Phone', 'Nokia',
'Palm', 'Samsung'
]
end
This will put all data from module's array into the checkbox drop-down as checkbox options. In My form I did:
<div class="card">
<a class="card-link card-header" data-toggle="collapse" href="#collapseOne">
Mobile Phones
</a>
<div id="collapseOne" class="collapse" data-parent="#accordion">
<div class="card-body">
<% Mobilenphones_Data::Activities.each do |activity| %>
<div id="skill_list" class="col-lg-3 col-md-4 col-sm-12 col-12">
<%= f.check_box :skills, { multiple: true }, activity, false %>
<%= activity %>
</div>
<% end %>
</div>
</div>
</div> <!--bottom-->
</div>
In my view "show.html.erb":
<% #name_of_model.skills.each do |skill| %>
<%= skill %>
<% end %>
For posterity sake:
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= f.label "Select a Product" %><br />
<%= f.check_box(:option) %>
<%= f.label(:mug, "Option") %><br />
<%= f.check_box(:option2) %>
<%= f.label(:mousepad, "Option2") %>
</div>
</div>
</div>

How to pass parameters in controller in Ruby On Rails

I am trying to pass parameters in controller but it is not returning anything.Please help me how I can pass parameters .
Controller Code -
def print_salary_slip_monthwise
#month = params[:salary][:month]
#year = params[:salary][:year]
#company = params[:salary][:company_id]
#company_location = params[:salary][:company_location_id]
#department = params[:salary][:department_id]
#salaryslips = Salaryslip.where(month: #month,year: #year.to_s,employee_id: #salary1)
end
form.html.erb - This is my form in which I used on change event .
<div class="box">
<div class="box-header">
<h3 class="box-title">Salary Slip Department Wise</h3>
</div><!-- /.box-header -->
<div class="box-body">
<%= bootstrap_form_for(:pdf_salaries, url: { action: 'print_salary_slip_monthwise'},html: {id: 'pdf_salaries'},remote: true ) do |f| %>
<div class="row">
<div class="col-sm-2">
<label>Year</label>
<div class="field">
<%= select :salary,:year,['2015','2016','2017','2018','2019','2020','2021','2022','2023','2024','2025','2026','2027'],{label: 'Select Year',include_blank: " Select Year"},{class: 'form-control'} %>
</div>
</div>
<div class="col-sm-2">
<label>Month</label>
<div class="field">
<%= select :salary,:month, ['January','February','March','April','May','June','July','August','September','October','November','December'],{label: 'Select Month',include_blank: " Select Month"}, class: "form-control" %>
</div>
</div>
<div class="col-sm-2">
<div class="form-group required">
<div class="input-group">
<%= f.select :company_id, all_company,{include_blank: "Select Company"},{onchange:"var a={id:$(this).val(), form : 'employee'}; $.get('/employees/collect_company_location',a,function(response){});"}%>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group required">
<div id="company_location">
<%= render 'employees/company_location_dropdown' %>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group required">
<div class="input-group">
<div id="department">
<%= render 'employees/department_dropdown' %>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<div class="actions">
<%= f.submit "Display Report", class: "btn btn-sm btn-primary",id: "buttonCtc" %>
</div>
</div>
</div>
<div class="ajax-progress"></div>
<div id="employee_list_pdf"></div>
</div>
</div>
<%end%>
You can pass parameters using form or using ajax. Once you pass a parameters to your server (controller) you can check it to your server log to see the parameters. In rails 4 you need to use Strong Parameters to save these parameters to your database.
Take this as an example of the Form will below field.
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
When above form will be submitted. you can see the rails server terminal for params
Parameters: {"user"=>{"username"=>"user1", "password"=>"password"}, "commit"=>"Submit"}
Access params in controller like this:
params[:user][:username] # code in controller
Hope this give you the clear understaning.

Rails form_tag is using GET instead of PUT

I'm using a form_tag to update an attribute in my 'Car' model. Everything should work except when I check the logs its using GET instead of the PUT like it should.
routes
apply_superadmin_apply_coupons_path - PUT /superadmin/apply_coupons/apply(.:format) superadmin/apply_coupons#apply
superadmin_apply_coupons_path - GET /superadmin/apply_coupons(.:format) superadmin/apply_coupons#index
view
<form class="super-admin-apply-coupons form-horizontal">
<%= form_tag(apply_superadmin_apply_coupons_path, action: 'apply', method: 'PUT') do %>
<div class="form-group">
<label class="col-sm-2"> Select car</label>
<div class="col-sm-4 apply-coupon-wrap">
<%= select_tag :id, options_from_collection_for_select(#cars, "id", "device_number"), class:"form-control" %>
</div>
</div>
<div class="form-group">
<label class="col-md-2">Select coupon code</label>
<div class="col-sm-4 apply-coupon-wrap">
<%= select_tag :coupon_code_id, options_from_collection_for_select(#coupon_codes, "id", "name"), class:"form-control" %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<%= submit_tag 'Apply', class: 'btn btn-success' %>
</div>
</div>
<% end %>
</form>
Its obviously just redirecting back to the index page at the moment without triggering the apply action I'm wanting it to.
You are probably producing two <form> tag in your view, one is the html form tag, the other is from <%=form_tag(...)%>. Just try to remove the html form tag might make it.

Trying to initiate a variable on page load Angularjs

I am trying to initiate a variable showName when the page loads. It worked fine when I did it using just angular but when I implemented it with rails(with embedded ruby tags) I am not able to hide the text field and save button as my variable gets undefined. I have tried couple of ways and ended with no result.
Here is my code. Any help is really appreciated. Thanks.
Edit.html.erb:
<div class="row" ng-controller="EditUserCtrl">
<div class="col-md-6 col-md-offset-3" id="editForm">
<%= form_for(#user) do |f| %>
<h1 id="editHeader">Account Information </h1>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, "Full Name:" %><br>
<div class="row">
<div class="col-sm-7">
<div ng-hide="showName">
<%= f.label :name, #user.name , id:"editFields" %>
</div>
<%= f.text_field :name, { :'ng-show' => 'showName'}%>
</div>
<div class="col-sm-5" ng-show="showName">
<%= f.button "Save", :action => "update_name" , :method => :put, class: "btn btn-primary", :'ng-click' => "clicked()", id:"editAccount" %>
</div>
<div class="col-sm-5" ng-hide="showName">
<i id="nav-cart-count" class="glyphicon glyphicon-edit"></i> Edit
</div>
</div>
</div>
<% end %>
</div>
Use ng-init to initialize the variable
<div class="row" ng-controller="EditUserCtrl" ng-init="showName = true">

Resources