Missing partial invoices/__invoice_item_fields - ruby-on-rails

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.

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.

Nil:nil class error on each loop when Form to build object loaded on page first

I have a form on a page that is building a “tip” for a “guide”. Below the form, I am running an each loop over all the #guide.tips. I keep getting an error as the each loop is loading the forms blank object as it sees it as a tip. I’ve currently solved the problem by using jQuery to inject the form after page load, but there has to be a better solution.
Each Loop on Guide.html.erb
<% if !#guide.tips.blank? %>
<% #guide.tips.each do |tip| %>
<div class="row mx-1">
<div class="col-md-3 border-right">
<div class="float-left">
<%= image_tag avatar_url(tip.user), class: "float-left text-left align-middle rounded img-fluid mx-3", width: "30%" %>
<p><%= tip.user.fullname %></p>
</div>
<div class="float-right mr-3">
<%= link_to like_tip_path(tip), method: :put do %>
<div class="fas fa-angle-up"></div>
<%= tip.get_upvotes.size %>
<% end %><br/>
<%= link_to dislike_tip_path(tip), method: :put do %>
<div class="fas fa-angle-down"></div>
<%= tip.get_downvotes.size %>
<% end %>
</div>
</div>
<div class="col-md-8 ml-3">
<h6><%= tip.title %> -<span class="ml-1"><small><%= tip.created_at.strftime("%A, %d %b %Y %l:%M %p")%></small></span></h6>
<%= tip.tip %>
</div>
</div><hr/>
<% end %>
<% end %>
Tips_Controller.rb
def new
#guide = Guide.find(params[:guide_id])
#tip = Tip.new
end
def edit
end
def create
#guide = Guide.find(params[:guide_id])
params[:tip][:user_id] = current_user.id
#tip = #guide.tips.create!(tip_params)
redirect_to guide_path(#guide)
end
Tip Form Partial
<%= form_for([#guide, #guide.tips.build]) do |f| %>
<div class="form-group row">
<div class="col-sm-12">
<p class="text-left">Tip Title</p>
<%= f.text_field :title, placeholder: "Enter Title", class: "form-control" %>
</div>
<div class="col-sm-12 mt-2">
<p class="text-left">Your Tip</p>
<%= f.text_area :tip, placeholder:"What's your tip for this travel guide?", class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-md">
<%= f.submit 'Submit', class: 'btn btn-primary btn-block' %>
</div>
</div>
<% end %>
Here is another solution that does not separate the logic between the controller and the view and cleans up your empty array statement for your loop
Each Loop on Guide.html.erb
<% #guide.tips.each do |tip| %>
<div class="row mx-1">
<div class="col-md-3 border-right">
<div class="float-left">
<%= image_tag avatar_url(tip.user), class: "float-left text-left align-middle rounded img-fluid mx-3", width: "30%" %>
<p><%= tip.user.fullname %></p>
</div>
<div class="float-right mr-3">
<%= link_to like_tip_path(tip), method: :put do %>
<div class="fas fa-angle-up"></div>
<%= tip.get_upvotes.size %>
<% end %><br/>
<%= link_to dislike_tip_path(tip), method: :put do %>
<div class="fas fa-angle-down"></div>
<%= tip.get_downvotes.size %>
<% end %>
</div>
</div>
<div class="col-md-8 ml-3">
<h6><%= tip.title %> -<span class="ml-1"><small><%= tip.created_at.strftime("%A, %d %b %Y %l:%M %p")%></small></span></h6>
<%= tip.tip %>
</div>
</div><hr/>
<% end unless #guide.tips.blank? %>
Tip Form Partial
By creating the Tip from outside the association (i.e. not using build), it will not be loaded into the class variable's children
<%= form_for([#guide, Tip.new(guide: #guide)]) do |f| %>
<div class="form-group row">
<div class="col-sm-12">
<p class="text-left">Tip Title</p>
<%= f.text_field :title, placeholder: "Enter Title", class: "form-control" %>
</div>
<div class="col-sm-12 mt-2">
<p class="text-left">Your Tip</p>
<%= f.text_area :tip, placeholder:"What's your tip for this travel guide?", class: 'form-control' %>
</div>
</div>
<div class="form-group row">
<div class="col-md">
<%= f.submit 'Submit', class: 'btn btn-primary btn-block' %>
</div>
</div>
<% end %>
Another Example / Q&A
Using the structure you already have you could do:
<% if #guide.tips.count > 1 %> (assuming this will always be shown with an empty "build" tip)
or you can use:
<% next unless tip.persisted? %>
inside the loop, before the actual form.
You also don't need the blank? check as an .each on an empty enumerator simply won't execute the block, and as long as there's a has_many relationship between guide and tips it will always yield a collection proxy.
I would write it as:
<% #guide.tips.each do |tip| %>
<% next unless tip.persisted? %>
<div class="row mx-1">
<div class="col-md-3 border-right">
...
<% end %>

Rails partial view repeat display

Here is part of my index.html
<div class="col-md-8">
<% #books.each do |book| %>
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
&nbsp
<% end %>
</div>
</div>
<% end %>
</div>
Basically, it will display 3 books, and it works fine.
Then, I move that code into _book.html.erb and edit above code into
<%= render #books %>
However, it repeat 3 times, that is, it display 9 books. And the sequence is [1st,2nd,3rd] [1st,2nd,3rd] [1st,2nd,3rd] like this picture.
Update
update index.html.erb
<div class="clearfix">
<div class="col-md-12">
<%= render #books %>
<div class="col-md-4">
<h3>Genre (Click to filter books)</h3>
<br>
<li>
<span class = 'label label-danger'>
<%= link_to "No filter" ,books_path, style: 'color:#FFFFFD' %>
</span>
<br>
<br>
</li>
<% #genres.each do |genres| %>
<li>
<span class = 'label label-primary' style="color:#FFFFFD">
<%= link_to genres.name ,books_path(filter: genres.name),style: 'color:#FFFFFD' %>
</span>
</li>
<br>
<% end %>
</div>
</div>
<!-- clearfix -->
</div>
Try the following:
<div class="col-md-8">
<%= render #books %>
</div>
And make sure <% #books.each do |book| %> and its <% end %> tag aren't in the partial.
Rendering Collections Docs
Edit
#index.html.erb
<div class="col-md-8">
<%= render #books %>
</div>
# _book.html.erb
<div class="row">
<div class="col-md-4">
<%= image_from_amazon(book.amazon_id) %>
</div>
<div class="col-md-8">
<h3 class = "text-info">
<%= book.title %>
</h3>
<br>
<em class ="text-muted">
written by <%= book.author %>
</em>
<br>
<br>
<p>
<%= book.description %>
</p>
<% book.genres.each do |genres| %>
<span class="label label-primary">
<%= genres.name %>
</span>
&nbsp
<% end %>
</div>
</div>

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.

Submitting a form doesn't work after time, only after refreshing

I have a Simple Form and i just realized that if edit the form long enough (5-10secs) the Submit button suddenly doesn't work.
If i refresh the page and quickly type some letters and the submit it, it works...
This behavior is active on everything in my app that has a scaffold form, for example my Devise form's are just fine.
I have no idea what is causing this behavior.. What am i missing ?
EDIT
This behavior occurs only in Google Chrome Browsers.
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<h1>Test Form</h1>
</div>
</div>
<div class="panel-body">
<%= form_for(#question) do |f| %>
<% if #question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% #question.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control", :autofocus => true,
placeholder: "#"%>
</div>
<div class="form-group">
<%= f.label :body %><br>
<%= f.text_area :body, class: "redactor redactor-box" %>
</div>
</div>
<div class="panel-footer">
<div class="form-group">
<%= f.submit "Post", class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
It seems that the problem was in the HTML.
I wrapped all my html tags with form_for and for now it seems to be working..

Resources