Ajax form not submitting - Rails 5 [duplicate] - ajax

This question already has answers here:
Rails submitting a form through ajax and updating the view
(4 answers)
Closed 5 years ago.
I am trying to submit the following form using ajax
<%= form_tag contact_form_submission_path, remote: true do %>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= text_field_tag :name, nil, required: true, class: 'form-control', placeholder: "Name" %>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<%= email_field :email, nil, required: true, class: 'form-control', placeholder: "Email" %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= text_area_tag :message, nil, required: true, class: 'form-control', placeholder: "message", rows: '12' %>
</div>
</div>
</div>
<%= submit_tag 'Submit', class: 'btn btn-brand btn-round pull-right btn-sm'%>
<% end %>
Routes
post "/contact_form_submission", to: "pages#contact_form_submission"
Controller
def contact_form_submission
puts 'Form submit action triggered'
end
contact_form_submission.js.erb
$("form")[0].reset();
I've used this exact setup on countless occasions, and it usually works fine. However this form will not submit. I'm not getting any terminal output at all when hitting submit, and there are no console errors.
Is there something elsewhere that is required for Ajax to work that I am overlooking?
Thanks in advance.

try to change
<%= form_tag contact_form_submission_path, remote: true do %>
with
<%= form_tag "/contact_form_submission", remote: true do %>
also check is there any filter before action contact_form_submission

Related

Rails 6 Action Text - Form Validation Errors

I'm using Action Text for a Web CRUD I've created. The form has two main attributes:
title: Título
content: Contenido
Here's my form:
<%= simple_form_for(#announcement) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<div class="form-group">
<%= f.input :title, input_html: { class: 'form-control' }, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"> <%= Announcement.human_attribute_name :cover %></label>
<%= f.input :cover, label: false, input_html: { class: 'form-control' }, as: :file, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"><%= Announcement.human_attribute_name :content %></label>
<%= f.rich_text_area :content, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"> <%= Announcement.human_attribute_name :is_active %></label>
<div class="custom-control custom-switch mr-2">
<%= f.check_box :is_active, class: 'custom-control-input', id: 'is-active-check' %>
<label class="custom-control-label" for="is-active-check" />
</div>
</div>
</div> <!-- END FORM INPUTS -->
<div class="form-actions">
<%= f.button :button, class: 'btn btn-primary mt-3' %>
</div>
<% end %> <!-- END FORM -->
In my model, I'm validating the presence of the fields:
validates :title, :content, presence: true
Problem:
When I submit an empty form the title field shows the expected validation errors. However the content field (Action Text) does not. The empty content field is preventing the record from being saved (this is OK), but like I said is not showing the error in the form.
Please take the following image as reference:
Question:
How can I show the validation errors for the content field?
The simple_form gem added support for rich text areas in version 5.0.2.
With this version, simply write
<%= f.label :content, as: :rich_text_area %>
instead of
<%= f.rich_text_area :content %>
and simple_form will work its magic.

Error in partial layouts in rails 4

I have this view:
<%= form_for(:subject, :url => { :action => 'update', :id => #subject.id}) do |f| %>
<%= render partial: "layouts/form" , locals: {:jex => f} %>
And this form partial:
<div class="form-group">
<label class="col-sm-2 control-label ">Name</label>
<div class="col-sm-10 form-padding "><%= jex.text_field(:name ,:class => "form-control") %></div>
</div>
But, I have an error:
What am I doing wrong?
try
<%= render "layouts/form", jex: f %>

Post List of Values to model params

I am creating instructors page which have multiple course ids to be passed to the model parameter list.
The following is my code for whitelist params
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, {:courses_ids => []})
end
My create page is like below
<%= form_for #instructor, :html => { :class => "form-horizontal instructor" } do |f| %>
<% if #instructor.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#instructor.errors.count, "error") %> prohibited this instructor from being saved:</h2>
<ul>
<% #instructor.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :LastName, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :LastName, :class => 'form-control' %>
<%= error_span(#instructor[:LastName]) %>
</div>
</div>
<div class="form-group">
<%= f.label :FirstMidName, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :FirstMidName, :class => 'form-control' %>
<%= error_span(#instructor[:FirstMidName]) %>
</div>
</div>
<div class="form-group">
<%= f.label :HireDate, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :HireDate, :class => 'form-control' %>
<%= error_span(#instructor[:HireDate]) %>
</div>
</div>
<div class="form-group">
<%= f.label text="Courses", :class => 'control-label col-md-2' %>
<div class="col-md-10">
<table cellpadding="5">
<tr>
<td>
<input type="checkbox" name="course_ids" value="Course1">Course1
<input type="checkbox" name="course_ids" value="Course2">Course2
<input type="checkbox" name="course_ids" value="Course3">Course3
</td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
instructors_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
But if select multiple check-boxes params not passing as arrays. ?It took only the last one and showing not permitted. Please guide me. I don't have any attribute named course_ids in Instructor model.
To receive an array of values, you need to change the name of your checkboxes to be course_ids[]
In your permitted params, {:courses_ids => []} can be written simply as course_ids: [] (also notice the typo you had with the plural 'courses').

bootstrap navbar search embedded ruby

I have this code for a search bar in my nav bar.
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search an Artist">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
And I want it to correlate to this embedded ruby code
<%= form_tag(artists_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Artists" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
I tried to just remove the div class ="form-group" and submit button and then put the ruby code within the form class, but then it changes the look of it and will only work when I'm already on my artists page, I want it to work from anywhere on my site. Any suggestions on how to integrate the ruby code into the html one?
This should work:
<%= form_tag(artists_path, :method => "get", id: "search-form", html: {class: 'form-group'}) do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Artists", class: "form-control" %>
<%= submit_tag "Search", :class => 'btn btn-default',:name => nil%>
<% end %>

Rails link_to Remote Put Method Not refreshing throwing Nihlclass

I have a Rails 3.2.14 app where I have calls, calls have units, units have statuses, and calls times in the model such as in_service_time.
I'm trying to write a controller action called in_service which updates the unit's status to "In Service" and time stamps call.in_service_time to Time.zone.now and does all of this via put remote (Ajax).
What I've written so far seems to work by updating the unit status and time stamping the call.in_service_time. But using :remote => true doesn't render the partials as I've specified in the in_service action. I have to wait for the screen to refresh via a getScript call in my index.html.erb for the call status to update. If I use :remote => false it reloads the page immediately without problems. If I use the :remote => true I also throw an exception in my development.log for NihlClass.
Below are excerpts of the code in question, the full code can be found here to keep things easier to read: full code
index.html.erb
<div id="active">
<%= render "assigned_calls" %>
</div>
<div id="inactive">
<%= render "unassigned_calls" %>
</div>
<script>
$(function() {
setInterval(function(){
$.getScript("/calls").fail(function(jqxhr, settings, exception) {
window.location = "/users/sign_in?duplicate_session=true";
});
}, 10000);
});
</script>
index.js.erb
$("#active").html("<%= escape_javascript render("assigned_calls") %>");
$("#inactive").html("<%= escape_javascript render("unassigned_calls") %>");
routes.rb excerpt
resources :calls do
member do
post 'close'
post 'cancel'
post 'note'
get 'opencall'
get 'new_return'
get 'duplicate_call'
get 'edit_times'
put 'update_billing'
post 'dispatch_call'
put 'en_route'
put 'on_scene'
put 'to_hospital'
put 'at_hospital'
put 'in_service'
end
calls_controller.rb excerpt
def in_service
#call = Call.find(params[:id])
#unit = #call.units.first
#call.update_attributes(in_service_time: Time.zone.now)
#call.save
#unit.status = Status.find_by_unit_status("In Service")
#unit.save
respond_to do |format|
format.html { redirect_to calls_url }
format.js { render "index" }
end
end
_assigned_calls.html.erb
<div class="page-header">
<span class="badge badge-important"><%= #unassigned.count %></span> <strong>Unassigned calls</strong>
<span class="badge badge-info"><%= #assigned.count %></span> <strong>Active calls</strong>
<span class="badge badge-warning"><%= #scheduled.count %></span> <strong>Scheduled calls</strong>
<%= render "search" %>
<h2>Active Calls</h2>
</div>
<% #assigned.each do |call| %>
<div class="widget">
<div class="widget-header">
<div class="pull-right">
<%= link_to 'View', call, :class => 'btn btn-close'%>
<% if dispatch? %>
<%= link_to 'Edit', edit_call_path(call), :class => 'btn btn-close'%>
<%= link_to 'Close', close_call_path(call), confirm: 'Are you sure you want to close the call?', :method => :post, :class => 'btn btn-danger' %>
<%= link_to 'Cancel', cancel_call_path(call), confirm: 'Are you sure you want to cancel the call?', :method => :post, :class => 'btn btn-warning' %>
<% end %>
</div>
<i class="icon-phone"></i>
<h3><%= link_to call.incident_number, call %> <span><%= status(call) %></span></h3>
<% if call.traffic_type == "Emergency" %>
<span class="badge badge-important"><%= call.traffic_type %></span>
<% else %>
<span class="badge badge-info"><%= call.traffic_type %></span>
<% end %>
</div>
<div class="widget-content">
<div class="row">
<div class="span3">
<h4>Patient Name: <small><%= call.patient_name %></small></h4>
<div class="large-display">
<% if call.call_status == "open" %>
<div class="large <%= elapsed_overdue(call.elapsed_time) %>"><%= TimeFormatter.format_time(call.elapsed_time)%></div>
<div class="small">Elapsed Time</div>
<% else %>
<% if call.closed_at? %>
<div class="large <%= elapsed_overdue(call.run_time) %>"><%= TimeFormatter.format_time(call.run_time)%></div>
<div class="small">Run Time</div>
<% end %>
<% end %>
</div>
</div>
<div class="span2">
<address>
<strong><%= transferred_from(call) %></strong><br>
<%= transferred_from_address(call) %>
</address>
</div>
<div class="span1"><i class="icon-arrow-right dim"></i></div>
<div class="span2">
<address>
<strong><%= transferred_to(call) %></strong><br>
<%= transferred_to_address(call) %>
</address>
</div>
<div class="span3">
<% if call.service_level.level_of_service == "WC" %>
<div class="left-icon dim"><i class="icon-user"></i></div>
<% else %>
<div class="left-icon dim"><i class="icon-ambulance"></i></div>
<% end %>
Assigned to <strong>Unit <%= call.units.map(&:unit_name).join(", ") %></strong><br />
<% call.units.each do |unit| %>
<div class="<%= set_status(unit.status) %>"><%= unit_status(unit) %></div>
<% end %>
</div>
</div>
<hr />
<div class="row">
<div class="span2">
<div class="large-display">
<div class="medium <%= transfer_due(call.transfer_date) %>"><%= call.transfer_date.strftime("%m/%d/%y %H:%M") %></div>
<div class="small">Transfer Date</div>
</div>
</div>
<div class="span1">
<div class="large-display">
<div class="small"><%= call.nature.try(:determinant) %></div>
<div class="small">Nature</div>
</div>
</div>
<div class="span5">
<% if call.unit_ids.present? %>
<div class="large-display">
<div class="progress progress-striped"><%= progress_bar(call) %></div>
<div class="small"><%= call.units.first.status.unit_status %></div>
<div><%= link_to 'En Route', en_route_call_path(call), :class => 'btn btn-warning btn-medium', :method => :put, :remote => true %><%= link_to 'On Scene', on_scene_call_path(call), :class => 'btn btn-primary btn-medium', :method => :put, :remote => true %><%= link_to 'To Hospital', to_hospital_call_path(call), :class => 'btn btn-warning btn-medium', :method => :put, :remote => true %><%= link_to 'At Hospital', at_hospital_call_path(call), :class => 'btn btn-danger btn-medium', :method => :put, :remote => true %><%= link_to 'In Service', in_service_call_path(call), :class => 'btn btn-success btn-medium', :method => :put, :remote => true %></div>
<div>ER: <%= call.en_route_time.try(:strftime, "%m/%d/%y-%k:%M") %> OS: <%= call.on_scene_time.try(:strftime, "%m/%d/%y-%k:%M") %> TO: <%= call.to_hospital_time.try(:strftime, "%m/%d/%y-%k:%M") %> AT: <%= call.at_hospital_time.try(:strftime, "%m/%d/%y-%k:%M") %> IS: <%= call.in_service_time.try(:strftime, "%m/%d/%y-%k:%M") %></div>
</div>
<% end %>
</div>
<div class="span3">
<div class="left-icon dim"><i class="icon-user"></i></div>
<%= render partial: "medics_for_call", locals: {call: call} %>
</div>
</div>
</div>
</div>
<% end %>
development.log excerpt
NoMethodError - undefined method `count' for nil:NilClass:
app/views/calls/_assigned_calls.html.erb:2:in `_app_views_calls__assigned_calls_html_erb___2549181816739942207_70125964034780'
You're never setting #unassigned in your in_service action. So sending count to it throws an exception.
Rendering an action's view (render "index") from a different action does not execute the action's controller code.
EDIT: How to make it DRY:
class CallsController < ApplicationController
...
def index
setup_scheduling_variables
#units = Unit.order("unit_name")
#overdue = #assigned.select{|call| call.elapsed_time > 3600}
#inservice = Unit.in_service
end
def in_service
...
respond_to do |format|
format.html { redirect_to calls_url }
format.js do
setup_scheduling_variables
render "index"
end
end
end
private
def setup_scheduling_variables
#assigned = params[:search].present? ? Call.search(params[:search]) : Call.assigned_calls.until_end_of_day.order("transfer_date ASC")
#unassigned = Call.unassigned_calls.until_end_of_day
#scheduled = Call.scheduled_calls
end

Resources