Form with tabs - The data insert in tabs are not saving - ruby-on-rails

I have a form with tabs.
In each tab, there is a section.
The tabs its working, but I can't save the dates.
I insert datas in field's and I click in submit, but it's not working, the only filds saves are the fields in the first tab. (Details)
In case, I can get the value inserted in data_interview, interviewer and attachment.
I cant get the value inserted in name field.
The content insert in other tabs are lost.
![]
There is a part of code:
<div class="container">
<ul class="nav nav-tabs">
<li class="active"><%= t('labels.main_information') %></li>
<li><%= t('labels.personal_data') %></li>
<li><%= t('labels.formation') %></li>
<li><%= t('labels.idioms_certifications') %></li>
<li><%= t('labels.professional_experience') %></li>
<li><%= t('labels.other_informations') %></li>
<li><%= t('labels.professional_assessment') %></li>
</ul>
<%= form_for(#hr_curriculum_intern, :html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
<div class="tab-content">
<div id="main_information" class="tab-pane active">
<div class="container">
<form role="form">
<div class="row">
<label class="col-md-2"><%= t('columns.date_interview')%></label>
<%#= f.label :date_interview, :class => "col-sm-2 control-label" %>
<div class="col-md-1">
<%= f.date_select :date_interview %>
</div>
</div>
<div class="row">
<label class="col-md-2"><%= t('attributes.interviewer')%>*</label>
<%#= mark_required(#hr_curriculum_intern,:interviewer) %>
<%#= f.label :interviewer, :class => "col-sm-2 control-label" %>
<div class="col-md-3">
<%= f.text_field :interviewer, :class => "input-block-level" %>
</div>
</div>
<div class="row">
<label class="col-md-2"><%= t('attributes.attachment')%></label>
<%#= f.label :attachment, :class => "col-sm-2 control-label" %>
<div class="col-md-3">
<%= f.file_field :attachment, :class => "input-block-level" %>
</div>
</div>
</form>
</div>
</div>
<div id="personal_data" class="tab-pane" >
<div class="container">
<form role="form">
<div class="row">
<label class="col-md-2"><%= t('attributes.name')%>*</label>
<%#= f.label :name, :class => "col-sm-2 control-label" %>
<div class="col-md-4">
<%= f.text_field :name, :class => "input-block-level" %>
</div>
</div>
Sorry for my english.
I don't understand why it's not working.

get rid of these: <form role="form"> for a start. the form_for is responsible for starting and ending form tags. These extras may well be interrupting the data.

Related

How can I rearrange my params to get the desired structure?

I am on a rails 5.07 app. I have some params coming in from a form and that forms partial. I need to restructure these params or re-wire my model associations or try to implement Nested Forms...WHATEVER incantation I need to do in order to restructure / reorder the params from...
Note: I'm not sure how I can be more specific or ask to do only one thing here. As I am asking to do "only one thing"....sooo before you close my question....please be specific as to why? and give an example of how I can ask...this one thing... to rearrange params, differently?
current params
> {"utf8"=>"✓",
> "assignment"=>{"volunteer_shift_attributes"=>{"volunteer_task_type_id"=>"41",
> "roster_id"=>"7", "program_id"=>"9", "set_description"=>"Nov10",
> "set_date"=>"2021-01-08"}, "start_time(1i)"=>"2021",
> "start_time(2i)"=>"11", "start_time(3i)"=>"10",
> "start_time(4i)"=>"08", "start_time(5i)"=>"00",
> "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"10",
> "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"Nov10",
> "contact_id"=>"166574", "closed"=>"0", "lock_version"=>"0"},
> "contact_element_prefix"=>"contact", "commit"=>"Submit",
> "controller"=>"volunteer_events", "action"=>"create_shift"}
to Desired params structure
{"assignment"=>{"volunteer_shift_attributes"=>
{"volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9",
"set_description"=>"fonso says wtf?"}, "set_date"=>"2021-01-07",
"contact_id"=>"166574", "closed"=>"0", "start_time(4i)"=>"12",
"start_time(5i)"=>"00", "end_time(4i)"=>"13", "end_time(5i)"=>"00",
"notes"=>"fuuuuuuuuuk"}, "lock_versions"=>["0"],
"contact_element_prefix"=>"contact", "commit"=>"Update",
"controller"=>"volunteer_events", "action"=>"create_shift"}
so I can say something like...
params["assignment"]["volunteer_shift_attributes"]["roster_id"]
and result being "7"
Please let me know if I need to include my model associations or anything else. Happy to oblige.
UPDATE:
per request, adding the form and the partial below...
here is the main form that produces the undesired outcome
<!--This is NEW action form-->
<%= form_for #assignment, :url => #my_url, remote: true do |f| %>
<!-- #FIXME need a fields_for 4 the volunteer_event-->
<div class="">
<div class="modal-body d-flex">
<div class="col-sm-8 border-right">
<section id="location-date-time-notes" class="flex">
<% if #assignment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#assignment.errors.count, "error") %> prohibited this assignment from being saved:</h2>
<ul>
<% #assignment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<!--VOLUNTEER SHIFT-->
<!--TODO: make this a partial under field_for-->
<%= f.fields_for :volunteer_shift do |builder| %>
<%= render 'assignments/volunteer_shift_fields', vs: builder %>
<% end %>
<!--TODO: Volunteer Shift end -->
<div id="time-row" class="d-flex flex-row">
<label for="assignment_time" class="col-sm-3 p-2">
<i class="fa fa-clock-o fa-lg" aria-hidden="true"></i> Time:
</label>
<div class="col- p-2">
<div class="myStartTime" id="start_time_<%= #assignment.id %>">
<%= f.time_select :start_time %>
</div>
</div>
<div class="col- p-2"><i class="fa fa-arrows-h fa-lg" aria-hidden="true"></i></div>
<div class="col-sm-3 p-2">
<div class="myEndTime" id="end_time_<%= #assignment.id %>">
<%= f.time_select :end_time %>
</div>
</div>
</div>
<div class="d-flex flex-row">
<label for="assignment_notes" class="col-sm-3 p-2">
<i class="fa fa-clipboard fa-lg" aria-hidden="true"></i> Notes:
</label>
<div class="col-sm-9 p-2">
<div class="d-flex flex-row">
<span> Notes only get saved if a contact is assigned the shift, and get removed when the contact is removed from the shift.</span>
<div class="">
<%= f.label :notes %>
<%= f.text_area :notes %>
</div>
</div>
</div>
</div>
</section>
</div>
<div class="col-sm-4">
<!-- Contact Section-->
<div id="contact_section">
<% if #assigned_contacts && #assigned_contacts.length > 0 %>
<h2>Previously Assigned Contacts</h2>
<% #assigned_contacts.each do |c| %>
<%= label_tag "assigned_contacts[#{c.id}]", "Are you sure you want to remove the currently scheduled volunteer, #{c.display_name} (##{c.id}), from the assignment(s)?" %>
<%= check_box_tag "assigned_contacts[#{c.id}]", "replace", #replaced_contacts.include?(c.id) %>
<% end %>
<% end %>
<input id="contact_element_prefix" name="contact_element_prefix" type="hidden" value="contact">
<div class="name large flex-row">
<%= f.label :contact_id %><%= f.text_field :contact_id %>
</div>
<div id="display-contact" class="d-flex flex-row">
<% if f.object.contact_id %>
<%= render partial: 'contacts/contact_display', locals: { contact:f.object.contact} %>
<% else %>
<div>no contact attatched- _form.html called</div>
<%#= link_to 'Show Contact', contact_path(f.object.contact_id), remote: true %>
<% end %>
</div>
<!-- FIXME: replace this logic stack with AJAX-->
<%#= contact_field("#obj", "contact_id",
:locals => {:options => {
:object_name => f.object_name.to_s,
:field_name => 'contact_id',
:on_display => 'display_disciplinary_notes(); display_contact_notes();'
}}
) %>
<%= f.label :closed, "Is this slot closed?" %>
<%= f.check_box :closed %>
<!--Contact Section END-->
<!--Attendance / Call Status start-->
<% if f.object.id && f.object.contact_id %>
<div class="flex-row">
<div class="col-25"><label for="assignment_attendance_type_id">Attendance:</label></div>
<div class="col-75"><%= select(f.object_name,
"attendance_type_id",
AttendanceType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] },
:include_blank => true) %></div>
</div>
<div class="flex-row">
<div class="col-25"><label for="assignment_call_status_type_id">Call status:</label></div>
<div class="col-75"><%= select(f.object_name,
"call_status_type_id",
([["not called yet", ""]] + CallStatusType.all.sort_by(&:id).collect {|p| [ p.name, p.id ] }),
:include_blank => false) %></div>
</div>
<% end %>
<!-- Attendance / Call Status End-->
<!-- LOCK VERSION-->
<div class="flex-row">
<div class="col-25">
<%= f.label :lock_version %>
</div>
<div class="col-75">
<%= f.number_field :lock_version %>
</div>
</div>
<!-- LOCK end-->
</div>
</div>
</div>
<div class="modal-footer d-flex justify-content-between">
<div class="edit_icons d-flex flex-row">
<div class="d-flex flex-column">
<!-- <i class="fa fa-share-alt-square fa-lg" aria-hidden="true"></i> Split-->
<!-- <i class="fa fa-files-o fa-lg" aria-hidden="true"></i> Copy-->
</div>
<div class="d-flex flex-column">
<%#= link_to '<i class="fa fa-pencil-square-o fa-lg" aria-hidden="true"></i>Edit'.html_safe, edit_assignment_path, remote: true%>
<!-- <i class="fa fa-refresh fa-lg" aria-hidden="true"></i> Reassign-->
</div>
</div>
<div>
<button type="button" class="btn btn-secondary mr-2" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary"><%= f.submit "Submit" %></button>
<!-- <input id="assignment_submit" name="commit" type="submit" value="Update">-->
</div>
</div>
</div>
<% end %>
and the partial file _volunteer_shift_fields
<!--<div class="name large flex-row">-->
<%= vs.label :volunteer_shift %>
<!--</div>-->
<div id="volunteer_shift" class="d-flex flex-row">
<div class="col-sm-12 p-2">
<div id="volunteer_shift" class="text-right">
<div class="field">
<%= vs.label :volunteer_task_type_id %>
<%= vs.select 'volunteer_task_type_id', options_from_collection_for_select([VolunteerTaskType.new(:description => ""), VolunteerTaskType.instantiables.effective_on(Date.today)].flatten, "id", "description") %>
</div>
<div class="field">
<%= vs.label :roster_id %>
<%= vs.select 'roster_id', options_from_collection_for_select([Roster.new(:name => ""), Roster.all].flatten, "id", "name") %>
</div>
<div class="field">
<%= vs.label :program_id %>
<%= vs.select 'program_id', options_from_collection_for_select([Program.new(:name => ""), Program.where(:volunteer => true)].flatten, "id", "name")%>
</div>
<div class="field">
<%= vs.label :set_description, "Description" %>
<%= vs.text_area(:set_description) %>
</div>
<div class="field">
<%= vs.label :set_date, "Date" %>
<%= vs.text_field(:set_date) %>
</div>
</div>
</div>
</div>
UPDATE 2:
Big thanks to sam. adjusting my partial has gotten me 90% there. I "think" I just need to move the remaining form elements from parent to child and vice versa to get the rest of the way there. I'll post the outcome once I've tried that as well.
in your _volunteer_shift_fields file, try to bind your form element with the form object. Replace select_tag with vs.select, text_field with vs.text_field. Then Rails will automatically build the desired params.

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.

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">

Missing partial invoices/__invoice_item_fields

i have invoice and invoice_items models in my application. and i have used cocoon gem for nested models.i am using rails 4.2. it is working properly when i am creating new invoice, but when i am clicking on my edit button i am getting "template missing error" though i have _invoice_item_fields.html.erb file in my application.
this is my _form.html.erb file
<div class=" form">
<%= form_for(#invoice,:html=>{:class=>"cmxform form-horizontal tasi-form",:novalidate=>"novalidat"}) do |f| %>
<% if #invoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
<ul>
<% #invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group ">
<%= f.label :billing_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :billing_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :shipping_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :shipping_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :company_id ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :company_id,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :invoice_date ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.date_select :invoice_date,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :status ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :status,:class=>"form-control" %>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
Invoice Items
<span class="pull-right">
<%= link_to_add_association 'Add Item', f, :invoice_items,:class=>"btn btn-default"%>
</span>
</header>
<div class="panel-body">
<div class="adv-table">
<%= f.fields_for :invoice_items do |item| %>
<%= render '_invoice_item_fields', :f => item %>
<% end %>
</div>
</div>
</section>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= f.submit "Save",:class=>"btn btn-danger"%>
</div>
</div>
<% end %>
</div>
this is my _invoice_item_fields.html.erb file
This...
<%= render '_invoice_item_fields', :f => item %>
must be
<%= render 'invoice_item_fields', :f => item %>
You do not use a leading _ in your Rails render call to render a partial. The underscore goes on the filename on disk only.

Routing problems with STI

I've declared an STI on Works
class Work < ActiveRecord::Base
validates :title, presence: true
validates :visibility, presence: true
belongs_to :category
end
class Story<Work
end
class Poem<Work
end
Code for form:
<div class="panel panel-blue margin-bottom-40">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon-tasks"></i> Item Details</h3>
</div>
<div class="panel-body">
<%= form_for(#work, :html =>{:class =>"margin-bottom-40 new_item ",:multipart => true}) do |f| %>
<% if #work.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#work.errors.count, "error") %> found </h2>
<ul>
<% #work.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label "Title",:class=>"control-label" %>
<div class="controls">
<%= f.text_field :title,:class=>"form-control",:placeholder=>"Title of the story/poem" %>
<div class="description">
<div class="">
<strong>Enter a suitable title for your story/poem
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :type,:class=>"control-label" %>
<div class="controls">
<%= f.select :type,{"Story"=>"Story","Poem"=>"Poem"},{},:class=>"form-control",:placeholder=>"Story/Poem" %>
<div class="description">
<div class="">
<strong>Is it a story or a poem?
</strong>
</div>
</div>
</div>
</div>
<%
visibilities=["Me Only","Anyone with link","Logged In Users","Everyone"]
%>
<div class="form-group">
<%= f.label :visibility,:class=>"control-label" %>
<div class="controls">
<% [ 3,2, 1, 0 ].each do |option| %>
<br><%= radio_button_tag :visiblity, option, #visibility == option %>
<%= label_tag "visibility_#{option}", visibilities[option].humanize %>
<% end %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :category_id,:class=>"control-label" %>
<div class="controls">
<%= f.collection_select :category_id,Category.all,:id,:name,{},:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
Type your story/Poem here
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :content,:class=>"control-label" %>
<div class="controls">
<%= f.text_area :content,:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="actions">
<%= f.submit :class=>"btn btn-primary" %>
<%= link_to 'Back', works_path,:class=>"btn" %>
</div>
<% end %>
I have scaffolding for work with the parameter Type which accepts Story and Poem. When I submitted the form initially, I would get a stories_path not found error so I mapped stories and poems paths as follows:
resources :stories, :controller => 'works'
resources :poems, :controller => 'works'
So when I submit the first time, the form shows validation errors. When I submit it again after correcting validation, I get an exception
ActionController::ParameterMissing in WorksController#create
in this line:
params.require(:work).permit(:title, :type, :visibility, :category_id, :content)
I assume this is because its now renaming all the fields to story[] instead of work[]
How do I fix this and in the greater picture, how do I make STI work on the same controller. I just want to be able to use Poem.all and Story.all for fetching records.
Submisson currently works through the same interface
I am allowing the user to select the type of work in the form.

Resources