Action Listener on ruby on rails? - ruby-on-rails

I have this project: https://todo-app-back-end-prospeed.c9users.io/
It's basically a reminder web app. I trying to add a function in which the user clicks on the checkbox and the value <Todo id: 24, description: "Publish project", pomodoro_estimate: 1, complete: nil the complete: nill changes to complete: true or false `
<body>
<div class="container2">
<h1 id ="list">Workshop Todo List</h1>
<ul>
<% #todos.each do |todo| %>
<li>
<label class="container">
<input type="checkbox"/ checked="checked">
<%= todo.description %>
<span class="pomodoro-estimate"><%= todo.pomodoro_estimate %> pomodoro</span>
<span class="checkmark"></span>
</label>
</li>
<% end %>
</ul>
<a class="noColor" href="/new/"><input class="add-new-todo-button" type="submit" value="Add todo"/></a>
<h1 id="Pomodoro-technique">What is a pomodoro estimate?</h1>
<p id="Pomodoro-technique-info">
<strong>1 pomodoro = 25 minutes + 5 minutes break.</strong><br>
For more info: https://www.focusboosterapp.com/the-pomodoro-technique
</p>
</div>
</body>
ToDoController:
class TodoController < ApplicationController
def index
#todos = Todo.all
end
def show
# Gets number user typed in URL
todo_id = params[:id]
#Grab the todo with that id from the database
#todo = Todo.find_by_id(todo_id)
end
def new
#
end
def create
t = Todo.new
t.description = params['description']
t.pomodoro_estimate = params['pomodoro_estimate']
t.save
redirect_to "/show/#{t.id}"
end
def destroy
t = Todo.find_by_id(params[:id])
t.destroy
redirect_to "/"
end
def edit
#todo = Todo.find_by_id(params[:id])
end
def update
t = Todo.find_by_id(params['id'])
t.description = params['description']
t.pomodoro_estimate = params['pomodoro-estimate']
t.save
redirect_to "/show/#{t.id}"
end
end
What kind of controller would I need? How does it know when It is clicked?
I would just like to be pointed in the right direction

Your checkbox in your view is not good I think. You need something like:
<%= f.check_box: field, {}, true %>

Related

Rails5: getting a form submission error and confused as to why [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Ok, so I'm trying to update an old app by rebuilding it in a seperate Rails 5 app that points to the same database (well not really the --same-- but a clone of that DB)
I've recently tried using a nested form field approach to solve my "create new record" problem for this particular form but got jammed up for some reason....
soooooo...
I've gone back and tried a different approach by building a new event in the controller new action called "add_shift" and assigning each field individually. Then in my "create" using a method called create_shift.
NOTE: this was the technique used in the old app as well so I'm trying to port it over to the newer app...here is the error I am getting
the error
Started POST "/assignments" for blah-my-ip at 2021-11-08 06:48:49 -0800
Cannot render console from blah-my-ip! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by AssignmentsController#create as JS
Parameters: {"utf8"=>"✓", "volunteer_task_type_id"=>"41", "roster_id"=>"7", "program_id"=>"9", "set_description"=>["fonso nov8 test"], "set_date"=>["2021-01-05"], "assignment"=>{"start_time(1i)"=>"2021", "start_time(2i)"=>"11", "start_time(3i)"=>"8", "start_time(4i)"=>"08", "start_time(5i)"=>"00", "end_time(1i)"=>"2021", "end_time(2i)"=>"11", "end_time(3i)"=>"8", "end_time(4i)"=>"09", "end_time(5i)"=>"00", "notes"=>"Nov888", "contact_id"=>"166574", "closed"=>"0", "lock_version"=>"0"}, "contact_element_prefix"=>"contact", "commit"=>"Submit"}
VolunteerTaskType Load (0.2ms) SELECT "volunteer_task_types".* FROM "volunteer_task_types" WHERE "volunteer_task_types"."id" = $1 LIMIT $2 [["id", 41], ["LIMIT", 1]]
Roster Load (0.1ms) SELECT "rosters".* FROM "rosters" WHERE "rosters"."id" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
Program Load (0.1ms) SELECT "programs".* FROM "programs" WHERE "programs"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Rendering assignments/new.html.erb within layouts/application
Rendered assignments/new.html.erb within layouts/application (3.6ms)
Rendered layouts/_messages.html.erb (0.6ms)
Completed 422 Unprocessable Entity in 1202ms (Views: 269.7ms | ActiveRecord: 11.5ms)
....below are my controller, models and views related to this form...
class AssignmentsController < ApplicationController
before_action :set_assignment, only: [:show, :edit, :update, :destroy]
skip_before_action :verify_authenticity_token #TODO refactor this line to be very specific
# GET /assignments or /assignments.json
def index
#assignments = Assignment.date_range(params[:start]..params[:end])
end
# GET /assignments/1 or /assignments/1.json
def show
end
# GET /assignments/new
def new
add_shift
# #assignment.volunteer_shift.build
#my_url = {:action => "create", :id => params[:id]}
end
# GET /assignments/1/edit
def edit
end
# POST /assignments or /assignments.json
def create
# #assignment = Assignment.new(assignment_params)
create_shift
respond_to do |format|
if #assignment.save
format.html { redirect_to #assignment, notice: "Assignment was successfully created." }
format.json { render :show, status: :created, location: #assignment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #assignment.errors, status: :unprocessable_entity }
end
end
end
def add_shift # FIXME: evil brought over from old app
ve = nil
if !params["id"].blank?
ve = VolunteerEvent.find_by_id(params["id"])
else
ve = VolunteerEvent.new
end
vs = ve.volunteer_shifts.new
vs.program = Program.find_by_name("intern")
# vs.slot_count = 1
vs.volunteer_event_id = ve.id if ve.id
vs.volunteer_event = ve
a = vs.assignments.new
a.volunteer_shift = vs
vs.stuck_to_assignment = true
vs.not_numbered = true
#assignments = vs.assignments = [a]
#referer = request.env["HTTP_REFERER"]
#my_url = {:action => "create_shift", :id => params[:id]}
#assignment = a
# binding.pry
# render :partial => 'assignments/new'
# render :partial => 'assignments/edit' #<--original
end
def create_shift # FIXME: evil brought over from original code base
# #fonso = #assignment.inspect
ve = nil
# Fixme: building volunteer shifts variable "vs" and associating with assignment
if !params["id"].blank?
ve = VolunteerEvent.find(params["id"])
else
if params["roster_id"].blank? || params["set_date"].blank?
ve = VolunteerEvent.new # won't save
else
ve = Roster.find_by_id(params["roster_id"]).vol_event_for_date(params["set_date"])
end
end
vs = ve.volunteer_shifts.new
vs.stuck_to_assignment = true
vs.not_numbered = true
#fixme: volunteer shifts variable "vs" and association with assignment end
# FIXME: vs.attributes=(params["assignment"]["volunteer_shift_attributes"]) # original needs to be rebuilt 4 this system
h0 = {"volunteer_task_type_id" => params["volunteer_task_type_id"]}
h1 = {"roster_id" => params["roster_id"]}
h2 = {"program_id" => params["program_id"]}
h3 = {"set_description" => params["set_description"]}
hash_arr = [h0,h1,h2,h3]
volunteer_shift_attributes = hash_arr.reduce { |acc, h| (acc || {}).merge h }
vs.attributes = volunteer_shift_attributes
#fixme: vs.attributes fix end
#FIXME: building variable - #assignments END
assignment = vs.assignments.new
vs = assignment.volunteer_shift
assignment.attributes = (assignment_params)
#assignments = [assignment]
#fixme: building variable - #assignments END
#FIXME: wtf is it and why is it?
vs.assignments = [assignment]
vs.set_values_if_stuck #fixme: <---- drill into this one
vs.assignments = []
binding.pry
#success = assignment.valid? && vs.save #fixme: <--------what was the valid? error here?
#assignment = assignment #fixme: <-----------------------#assignment is finally built here
# fixme: the above lines are merging params from one into the other in the old app.
# fixme: to create the new volunteer_shift.
if #success
vs = vs.reload
#assignment = a = vs.assignments.new
a.volunteer_shift = vs
# a.volunteer_shift_id = vs.id
a.attributes = (params["assignment"])
#assignments = vs.assignments = [a]
if !#success
vs.destroy
end
end
end
def update # ported over from old app
unless params[:assignment]
redirect_to :action => "index"
return
end
#my_url = {:action => "update", :id => params[:id]}
last_id = nil
begin
#assignments = params[:id].split(",").map{|x| last_id = x; Assignment.find(x)}
rescue ActiveRecord::RecordNotFound
flash[:jsalert] = "The assignment (##{last_id.to_i.inspect}) seems to have disappeared or never existed. It is possible somebody else has modified or deleted it."
rt = params[:assignment].delete(:redirect_to)
redirect_skedj(rt, "")
return
end
lv = params["lock_versions"]
ac = params["assigned_contacts"] || {}
#assigned_contacts = []
#replaced_contacts = []
ret = true
#assignments.each do |as|
as.lock_version = lv[as.id.to_s]
if as.lock_version_changed?
as.errors.add("lock_version", "is stale for this assignment, which means it has been edited by somebody else since you opened it, please try again")
ret = false
end
if as.contact_id && as.contact_id.to_s != params[:assignment][:contact_id].to_s
#assigned_contacts << as.contact
unless ac[as.contact_id.to_s] && ac[as.contact_id.to_s] == "replace"
as.errors.add("contact_id", "has been changed, please confirm below that the volunteer who is already assigned to the shift should be removed")
ret = false
else
#replaced_contacts << as.contact_id
end
end
end
rt = params[:assignment].delete(:redirect_to)
js_alert = nil
if ! ret
#assignment = Assignment.new
#assignment.volunteer_shift = #assignments.first.volunteer_shift
#assignment.attributes=(params[:assignment]) # .. ? .delete("volunteer_shift_attributes")
end
#assignments.each{|x|
if ret
#assignment = x
bc = x.contact_id
ret = !!(x.update_attributes(params[:assignment]))
if bc != x.contact_id and x.first_time_in_area?
alert = "#{x.contact.display_name} (##{x.contact_id}) has never logged hours for the #{x.volunteer_shift.volunteer_task_type.description} task type. Please remind the volunteer of the requirements for this area."
if x.volunteer_shift.volunteer_event and x.volunteer_shift.volunteer_event.notes and x.volunteer_shift.volunteer_event.notes.length > 0
alert += "\n\nSome suggested notes saved in the database for this event are:\n" + x.volunteer_shift.volunteer_event.notes
end
js_alert = alert
end
end
}
if ret && #assignment.contact and not #assignment.contact.is_old_enough?
msg = "This volunteer is not yet #{Default['minimum_volunteer_age']} years old (based on their saved birthday: #{#assignment.contact.birthday.to_s}).\nPlease remind the volunteer that they must have an adult with them to volunteer."
if js_alert == nil
js_alert = msg
else
js_alert = msg + "\n\n" + js_alert
end
end
flash[:jsalert] = js_alert if js_alert
if ret
flash[:notice] = 'Assignment was successfully updated.'
redirect_skedj(rt, #assignment.volunteer_shift.date_anchor)
else
#referer = rt
render :action => "edit"
end
end
# DELETE /assignments/1 or /assignments/1.json
def destroy
#assignment.destroy
# NOTE: comment original out 4 now
# respond_to do |format|
# format.html { redirect_to assignments_url, notice: "Assignment was successfully destroyed." }
# format.json { head :no_content }
# end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_assignment
#assignment = Assignment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def assignment_params
#fixme: ,volunteer_shift_attributes: [:???, :???, :???] <--- insert this below?
params.require(:assignment).permit(:title, :date_range, :set_date, :date, :volunteer_shift_id, :contact_id, :start_time, :end_time, :start, :end, :attendance_type_id, :notes, :call_status_type_id, :closed, :lock_version, :color, :description)
# params.require(:assignment).permit(:title, :redirect_to, :set_date, :date_range, :contact_id, :start_time, :end_time, :start, :end, :attendance_type_id, :notes, :call_status_type_id, :closed, :lock_version, :color, volunteer_shift_attributes: [:volunteer_task_type_id,:roster_id,:program_id,:set_description,:set_date,:id,:destroy])
end
end
Model assignment
class Assignment < ApplicationRecord
# attr_accessor :volunteer_event ,:contact_id #why is this volunteer_event and not volunteer_shift???
belongs_to :volunteer_shift
has_one :volunteer_task_type, :through => :volunteer_shift, :source => :volunteer_task_type
belongs_to :contact ,optional: true
validates_presence_of :volunteer_shift #belongs_to takes care of this now
validates_associated :volunteer_shift
belongs_to :attendance_type
belongs_to :call_status_type
validates_presence_of :set_date, :if => :volshift_stuck #belongs_to takes care of this now??
accepts_nested_attributes_for :volunteer_shift, allow_destroy: true
delegate :set_date, :set_date=, :to => :volunteer_shift
delegate :set_description, :set_description=, :to => :volunteer_shift
has_one :contact_volunteer_task_type_count, lambda{||
{:conditions => 'contact_volunteer_task_type_counts.contact_id = #{defined?(attributes) ? contact_id : "assignments.contact_id"}', :through => :volunteer_shift, :source => :contact_volunteer_task_type_counts}
}
scope :date_range, lambda { |range|
joins(volunteer_shift: :volunteer_event)
.where(volunteer_shifts: { volunteer_events: { date: range } })
}
scope :is_after_today, lambda {||
{ :conditions => ['(SELECT date FROM volunteer_events WHERE id = (SELECT volunteer_event_id FROM volunteer_shifts WHERE id = assignments.volunteer_shift_id)) > ?', Date.today] }
}
scope :on_or_after_today, lambda {||
{ :conditions => ['(SELECT date FROM volunteer_events WHERE id = (SELECT volunteer_event_id FROM volunteer_shifts WHERE id = assignments.volunteer_shift_id)) >= ?', Date.today] }
}
scope :not_cancelled, -> { where('(attendance_type_id IS NULL OR attendance_type_id NOT IN (SELECT id FROM attendance_types WHERE cancelled = \'t\'))')}
scope :roster_is_limited_by_program, -> {where("roster_id IN (SELECT id FROM rosters WHERE limit_shift_signup_by_program = 't')").joins(:volunteer_shift)}
def real_programs
return [] unless self.volunteer_shift&.roster
return [] unless self.volunteer_shift.roster.limit_shift_signup_by_program
self.volunteer_shift.roster.skeds.select{|x| x.category_type == "Program"}.map{|x| x.name}
end
attr_accessor :attendance_type_id
# TODO: find all time_range_s methods and either pull out to DRY or give unique names
def time_range_s
return "" unless start_time and end_time
(start_time.strftime("%I:%M") + ' - ' + end_time.strftime("%I:%M")).gsub( ':00', '' ).gsub( ' 0', ' ').gsub( ' - ', '-' ).gsub(/^0/, "")
end
def description
return unless volunteer_shift
self.volunteer_shift.volunteer_event.date.strftime("%D") + " " + self.time_range_s + " " + self.slot_type_desc
end
def roster_title
return unless volunteer_shift
self.volunteer_shift.roster.name
end
def date
return unless volunteer_shift
volunteer_shift.date
end
#full calendar uses this method name....see the assignment.json.jbuilder
def event_date
return unless volunteer_shift
self.date
end
def contact_display
if self.closed
"(closed)"
elsif contact_id.nil?
return "(available)"
else
self.contact.display_name + "(#{self.voltask_count})"
end
end
before_validation :set_values_if_stuck
def set_values_if_stuck
return unless (volshift_stuck || volunteer_shift)
volunteer_shift.set_values_if_stuck(self)
end
after_destroy { |record| if record.volunteer_shift&.stuck_to_assignment; record.volunteer_shift.destroy; else VolunteerShift.find_by_id(record.volunteer_shift_id).fill_in_available; end}
after_save {|record| if record.volunteer_shift&.stuck_to_assignment; record.volunteer_shift.save; end}
after_save { |record| VolunteerShift.find_by_id(record.volunteer_shift_id).fill_in_available }
def volunteer_shift_attributes=(attrs)
return unless volunteer_shift
self.volunteer_shift.attributes=(attrs) # just pass it up
end
def volshift_stuck
return unless volunteer_shift
self.volunteer_shift&.stuck_to_assignment
end
#for fullcalendar
def all_day_event?
self.start_time == self.start_time.midnight && self.end_time == self.end_time.midnight ? true : false
end
end
model volunteer_shift
class VolunteerShift < ApplicationRecord
validates_presence_of :roster_id
validates_presence_of :end_time
validates_presence_of :start_time
has_many :assignments
belongs_to :volunteer_default_shift
belongs_to :volunteer_task_type
belongs_to :roster
belongs_to :volunteer_event
belongs_to :program
has_many :contact_volunteer_task_type_counts, :primary_key => 'volunteer_task_type_id', :foreign_key => 'volunteer_task_type_id' #:through => :volunteer_task_type
def validate
errors.add("end_time", "is before the start time") unless self.start_time && self.end_time && self.start_time < self.end_time
end
...
def set_description
self.description
end
def set_description=(desc)
self.description=(desc)
end
def set_date_set
#set_date_set
end
def set_date=(val)
#set_date_set = true
#set_date = val
end
def set_date
#set_date_set ? #set_date : self.volunteer_event.date
end
def set_values_if_stuck(assn_in = nil)
return unless self.stuck_to_assignment #<---it's a boolean in the database
assn = assn_in || self.assignments.first
return unless assn
self.start_time = assn.start_time
self.end_time = assn.end_time
return unless self.volunteer_event_id.nil? or self.volunteer_event.description.match(/^Roster #/)
return unless set_date_set <----SOMETHING BREAKS HERE on these two lines
roster = Roster.find_by_id(self.roster_id)
if roster and !(set_date == nil || set_date == "")
ve = roster.vol_event_for_date(set_date)
ve.save! if ve.id.nil?
self.volunteer_event = ve
self.volunteer_event_id = ve.id
else
if self.volunteer_event.nil?
self.volunteer_event = VolunteerEvent.new
end
end
end
def shift_display
time_range_s + ((!(self.description.nil? or self.description.blank?)) ? (": " + self.description) : "")
end
def time_range_s
return unless self.read_attribute(:start_time) and self.read_attribute(:end_time)
(self.my_start_time("%I:%M") + ' - ' + self.my_end_time("%I:%M")).gsub( ':00', '' ).gsub( ' 0', ' ').gsub( ' - ', '-' ).gsub(/^0/, "")
end
def my_start_time(format = "%H:%M")
read_attribute(:start_time).strftime(format)
end
def self._parse_time(time)
Time.mktime(2000, 01, 01, *time.split(":").map(&:to_i))
end
def my_start_time=(str)
write_attribute(:start_time, VolunteerShift._parse_time(str))
end
def my_end_time(format = "%H:%M")
read_attribute(:end_time).strftime(format)
end
def my_end_time=(str)
write_attribute(:end_time, VolunteerShift._parse_time(str))
end
def self.range_math(*ranges)
... # have to edit this post down
end
def fill_in_available
return if self.stuck_to_assignment #<-- it's a boolean in the database
Thread.current['volskedj_fillin_processing'] ||= []
if Thread.current['volskedj_fillin_processing'].include?(self.id)
return
end
begin
Thread.current['volskedj_fillin_processing'].push(self.id)
Assignment.where(volunteer_shift_id: self.id).select{|x| x.contact_id.nil? and !x.closed}.each{|x| x.destroy}
inputs = [[(self.read_attribute(:start_time)), (self.read_attribute(:end_time))]]
Assignment.where(volunteer_shift_id: self.id).select{|x| !x.cancelled?}.each{|x|
inputs.push([(x.start_time), (x.end_time)])
}
results = self.class.range_math(*inputs)
results.each{|x|
a = Assignment.new
a.volunteer_shift_id, a.start_time, a.end_time = self.id, x[0], x[1]
a.volunteer_shift = self
a.closed = self.volunteer_event.nowalkins
a.save!
}
ensure
Thread.current['volskedj_fillin_processing'].delete(self.id)
end
end
after_save :fill_in_available
def date
self.volunteer_event.date
end
def date_display
self.date.strftime('%A, %B %d, %Y').gsub( ' 0', ' ' )
end
def date_anchor
self.date ? self.date.strftime('%Y%m%d') : ''
end
def time_shift(val)
self.start_time += val
self.end_time += val
end
def left_method_name
[self.volunteer_task_type_id.nil? ? self.volunteer_event.description : self.volunteer_task_type.description, self.slot_number].select{|x| !x.nil?}.join(", ")
end
def left_unique_value
left_method_name
end
def description_and_slot
((self.volunteer_task_type_id || -1) * 1000) + (self.not_numbered ? 0 : self.slot_number)
end
def weekday
Weekday.find_by_id(self.date.strftime("%w"))
end
end
And the views
_new.html.erb <----- NOTE this name...this is the modal
<div class="modal fade" id="add_event">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<div class="col-sm-12 left ">
<h4 class="modal-title">Add New Assignment - _new.html.erb</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
</div>
<%=#my_url %>
<%#= render 'assignments/form', assignment: #assignment %>
</div>
</div>
</div>
_form.html.erb
<!--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_event 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">
<p>_form</p>
<% if f.object.contact_id %>
<%= render partial: 'contacts/contact_display', locals: { contact:f.object.contact} %>
<% else %>
<div>no contact - _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 End-->
<% 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">
</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%>
</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>
</div>
</div>
</div>
<% end %>
_volunteer_shift_fields.html.erb
<!--<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">
<!-- old if: if class is assignment show volunteer shift else show default shift -->
<!-- we need default shift here...NO assignment is attached-->
<div class="field">
<%= vs.label :volunteer_task_type_id %>
<%= select_tag '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 %>
<%= select_tag 'roster_id', options_from_collection_for_select([Roster.new(:name => ""), Roster.all].flatten, "id", "name") %>
</div>
<div class="field">
<%= vs.label :program_id %>
<%= select_tag '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 %>
<%= text_field(:set_description, nil) %>
</div>
<div class="field">
<%= vs.label :set_date, "Date" %>
<%= text_field(:set_date, nil) %>
</div>
</div>
</div>
</div>
assignments/new.html.erb
Why is it coming here????
I just want the modal "_new.html.erb" to disapear after the form post but rails is sending me here. And I am confused as to why (something simple I'm sure)
<%= render 'form_old', assignment: #assignment %> <---this old form no longer exist
<!--
<%= link_to 'Back', assignments_path %>
volunteer_shift method set_values_if_stuck (see above also)
def set_values_if_stuck(assn_in = nil)
...
return unless self.volunteer_event_id.nil? or self.volunteer_event.description.match(/^Roster #/)
binding.pry
return unless set_date_set #fixme:<--- pry me
roster = Roster.find_by_id(self.roster_id)
...
end
Somewhat straightforward to me if I'm not mistaken your question as to "why _new is rendered".
def create
# #assignment = Assignment.new(assignment_params)
create_shift
respond_to do |format|
if #assignment.save
format.html { redirect_to #assignment, notice: "Assignment was successfully created." }
format.json { render :show, status: :created, location: #assignment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #assignment.errors, status: :unprocessable_entity }
end
end
end
Here, whatever happens in create_shift method, #assignment is being saved; if successfully, then render the :show page, if not, :new page, which by default _new.html.erb in this code.
As to "why the save fails", you could print or use one of those pry gems to see #assignment.errors right before #assignment.save.

Rails query: Find first record value and display in form

I'm currently trying to display the first record. The record is updated when someone decides to buy a wine. How would I get the first record for the specific wine ?
I thought of using .find(1)? but how would implement this into my controller ?
My current controller:
def create
wine = Wine.find(params[:wine_id])
if current_user == wine.user
flash[:alert] = "Du kannst nicht deinen eigenen Wein kaufen!"
else
start_date = Date.parse(reservation_params[:start_date])
#reservation = current_user.reservations.build(reservation_params)
#reservation.wine = wine
#reservation.price = wine.price
#reservation.in_stock = wine.in_stock - #reservation.bottle
#reservation.total = wine.price * #reservation.bottle
wine.update(in_stock: #reservation.in_stock)
# #reservation.save
if #reservation.save
if wine.Reservieren?
flash[:notice] = "Reserviert!"
else
#reservation.Versendet!
flash[:notice] = "Eroflgreich bestellt!"
end
else
flash[:alert] = "Can't make a reservation!"
end
end
redirect_to wine
end
My current view:
<% #wines.each do |wine| %>
<div class="row">
<div class="col-md-3">
<%= link_to wine.wine_name, wine_path(wine) %>
</div>
<div class="col-md-3">
<%= wine.in_stock %> <!-- Display first record -->
</div>
<div class="col-md-3">
<% if !wine.in_stock.blank? %>
Aktiv
<% else %>
Nicht Aktiv
<% end %>
</div>
<div class="col-md-3 right">
<%= link_to "Update", details_wine_path(wine), class: "btn btn-form" %>
</div>
</div><!-- row -->
<hr/>
<% end %>
Maybe you can explain a little better your code, because you receive #wines in your view and it is not coming from this controller action that you posted.
But if you want to take the first register you can basically call wines.first.
You would change from <%= wine.in_stock %> to <%= #wines.first %> for example.
Here is some thread related:
Display first record in rails

How do you Display Avarage Rating in index page?

I'm trying to display average ratings for jobs on my index page, it works perfectly fine on my show page
but on my index page the stars are there but are blank, how do i get them to display on my index page?
My Show Page:
<h4>Average Rating</h4>
<div class="average-review-rating" data-score=<%= #average_review %>></div>
<span class="number-of-reviews">Based on <%= #job.reviews.count %> reviews</span>
</div>
</div>
<script>
$('.review-rating').raty({
readOnly: true,
score: function() {
return $(this).attr('data-score');
},
path: '/assets/'
});
</script>
<script>
$('.average-review-rating').raty({
readOnly: true,
path: '/assets/',
score: function() {
return $(this).attr('data-score')
}
});
</script>
Jobs Show controller
def show
if #job.reviews.blank?
#average_review = 0
else
#average_review = #job.reviews.average(:rating).round(2)
end
end
My Index Page:
<% #jobs.each do |job| %>
<%= link_to job.title, job_path(job) %></h4>
<%= job.category %></p>
<%= job.city %>
<div class="average-review-rating" data-score=<%= average_review %>></div>
<span class="number-of-reviews">Based on <%= job.reviews.count %> reviews</span>
</div>
</div>
<% end %>
<script>
$('.average-review-rating').raty({
readOnly: true,
path: '/assets/',
score: function() {
return $(this).attr('data-score')
}
});
</script>
In your show page, you have #average_review defined. I'm guessing this was done in your jobs controller in the show action.
In your index page, you will need to calculate the average rating for each job as you are iterating through them. You can do this the same way you defined #average_rating. If you are defining your #average_rating in the show action as:
#average_rating = job.reviews.sum('score') / job.reviews.count
You will need to either define this method in the model (the better option), so something like:
app/models/job.rb
def average_review
reviews.sum('score') / reviews.count
end
Then in your index page:
<div class="average-review-rating" data-score=<%= job.average_review %>></div>
The other option is to set a variable for each object on the index page itself, less work but probably not as neat:
<% #jobs.each do |job| %>
<%= link_to job.title, job_path(job) %></h4>
<%= job.category %></p>
<%= job.city %>
<% average_review = job.reviews.sum('score') / job.reviews.count %>
<div class="average-review-rating" data-score=<%= average_review %>></div>
<span class="number-of-reviews">Based on <%= job.reviews.count %> reviews</span>
</div>
</div>
<% end %>
EDIT:
In your app/models/job.rb
def average_review
reviews.blank? ? 0 : reviews.average(:rating).round(2)
end
And index.html.erb
<div class="average-review-rating" data-score=<%= job.average_review %>></div>
You can fetch these results like so:
def index
#jobs = Job.all
#average_reviews = Review.where(job_id: #jobs)
.group(:job_id)
.average(:rating)
.transform_values { |rating| rating.round(2) }
end
This will return a hash with job ids as keys and the average rating as value (rounded to 2 decimals). To access these average ratings you can do the following in your index view:
#jobs.each do |job|
average_review = #average_reviews[job.id]
end
The above code only makes 2 SQL calls, 1 for fetching your jobs and 1 for fetching all average reviews for all the fetched jobs. Keep in mind that if a job has no reviews. When fetching the average rating from the hash the value nil is returned.

How to update a rails 5 variable using ajax

I have an application using Rails 5 and I have an article resource and I'm implementing "like/unlike" function. I already got that function but I'm having troubles on updating the #artlike variable on the view. These are my files:
show.html.erb:
<div class="pull-right">
<i class="fa fa-diamond"></i> <span id="likes"><%= render partial: "likes", locals: {artlike: #artlike} %></span>
</div>
Controller:
respond_to :js
def like
puts "LikesController:like"
#usuario = current_usuario
#articulo = Articulo.find(params[:id])
#usuario.like!(#articulo)
#artlike = Like.where(articulo_id: #articulo.id).count
end
def unlike
puts "LikesController:unlike"
#usuario = current_usuario
#like = #usuario.likes.find_by_articulo_id(params[:id])
#articulo = Articulo.find(params[:id])
#like.destroy!
#artlike = Like.where(articulo_id: #articulo.id).count
end
_likes.html.erb:
<span id="likes"><%= #artlike %></span>
like.js.erb File
$('span#likes').html('<%= j render partial: "likes", locals: {artlike: #artlike} %>');
How can I do the #artlike value in the view?
This is the ouput in the console log:
Console Log

Uninitialized constant error in my controller after adding a presenter

I added this to my controller:
class HtmlreportController < ApplicationController
def index
#report = Report.new
#report_presenter = ReportPresenter.new(#report_presenter)
end
end
Then added this presenter to /app/presenters
# app/presenters/htmlreport_presenter.rb
class ReportPresenter
def initialize(report)
#report = report
end
def pass_fail(view)
arrs = ['onemon.rb','twomon.rb','threemon.rb','fourmon.rb','fivemon.rb']
arrs.each do |arr|
shortname = File.basename("#{arr}", ".rb")
newest_file = Dir.glob("ScriptResults/#{shortname}/*").max
#reporter = File.readlines("/Users/username/Automation/Code/Reports/MonthlyTracking/#{newest_file}")
if #reporter.grep(/test failed/).any?
view.concat content_tag(:div, 'FAILED', class: 'results_fail')
else
view.concat content_tag(:div, 'PASSED', class: 'results_pass')
end
end
end
end
With this in my view:
<% title "HTML Report" %>
<!-- This is where the HTML Report lies -->
<h1>HTML Report for Marines.com Daily Monitoring</h1>
<div>View the grids below for the following results:</div>
<div id="results">
<div class="results_grid">
<div class="results_title">Xbox</div>
<%= #report_presenter.show_credentials(self) %>
</div>
</div>
But, I get this error when running it: uninitialized constant HtmlreportController::Report for the line #report = Report.new
How do I get it initialized to make it recognize the functions in my presenter into my view?

Resources