Form won't submit with AJAX - javascript

app/assets/javascripts/profiles.js
This is my javascript for the form. The jQuery hide/show are working but the AJAX request is not..
$(document).on('page:load ready', function() {
$('#pair-search').hide();
$('#date-search').hide();
$('input[type="radio"]').click(function() {
if ($(this).attr("value") == "pair") {
$("#pair-search").show();
$("#date-search").hide();
}
else {
$("#date-search").show();
$("#pair-search").hide();
}
$('#search-form').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
dataType:'script',
data: $(this).serialize(),
success: function(data) {
if (data) {
$('#profiles').html(data);
}
}
});
});
});
});
And here is the form on my profiles(#index) page ...it renders two partials. I will post the partial for "pair-search" so you can get an idea of how the forms work together.
app/assets/views/profiles/index.html.erb
User Search
<div id = "first_option">
<%= form_tag users_path, method: :get, id: 'search-form' do %>
<label>Who are you looking for?</label><br>
<input type="radio" id=pairchoice name="choice" value="pair"> Paired <br>
<input type="radio" id=datechoice name="choice" value="date"> Date <br>
<% end %>
</div>
<div class = "choices">
<div id = "pair-search">
<%= render 'pair_search' %>
</div>
<div id = "date-search">
<%= render 'date_search' %>
</div>
</div>
<div id = "profiles">
</div>
Pair search partial:
app/assets/views/profiles/_pair_search
<%= form_tag(profiles_path, method: :get, :id =>'search-form') do %>
<%= label(:language, 'What are your preferred languages?') %><br>
<%= check_box_tag('search[language]', 'Ruby') %>
<%= label_tag('search[language]', 'Ruby') %>
<%= check_box_tag('search[language]', 'JavaScript') %>
<%= label_tag('search[language]','JavaScript') %>
<%= check_box_tag('search[language]', 'Python') %>
<%= label_tag('search[language]', 'Python') %>
<%= check_box_tag('search[language]', 'Java') %>
<%= label_tag('search[language]','Java') %>
<%= check_box_tag('search[language]', 'PHP') %>
<%= label_tag('search[language]','PHP') %>
<%= check_box_tag('search[language]', 'C++') %>
<%= label_tag('search[language]','C++') %><br>
<%=label_tag(:operating_system,"What is your Operating System of choice?") %><br>
<%= check_box_tag('search[operating_system]', 'Unix') %>
<%= label_tag('search[operating_system]','Unix') %>
<%= check_box_tag('search[operating_system]', 'Linux') %>
<%= label_tag('search[operating_system]','Linux') %><br>
<%= label_tag(:age, 'What age-range are you looking for?')%><br>
<%= check_box_tag('search[age]', '20-25') %>
<%= label_tag('search[age]','20-25') %>
<%= check_box_tag('search[age]', '25-30') %>
<%= label_tag('search[age]', '25-30') %>
<%= check_box_tag('search[age]', '30-35') %>
<%= label_tag('search[age]', '30-35') %>
<%= check_box_tag('search[age]', '40-45') %>
<%= label_tag('search[age]','40-45') %><br>
<%= check_box_tag('search[age]', '50+') %>
<%= label_tag('search[age]','50+') %>
<%= label_tag(:gender, "Gender:") %>
<%= check_box_tag('search[male]') %>
<%= label_tag('search[male]', 'Male' )%>
<%= check_box_tag('search[female]')%>
<%= label_tag('search[female]', 'Female')%>
<%= check_box_tag('search[other]') %>
<%= label_tag('search[other]', 'Other')%>
<%= label_tag(:location, "Where do you want the person you're searching for to live?") %>
<%= text_field_tag ('search[location]') %>
<%= submit_tag("Search", :id => "search") %>
<% end %>

Related

Dynamically update rails form textarea based on dropdown select

I have a table called Email_Template. It contains two columns: template_name and template_body.
I also have a form in which user selects template_name in dropdown (coming from database column). In the same form, based on template_name selection, the textarea should display corresponding “template_body” content coming from the same database table. Please tell me how to do this.
My Form
<%= form_for :email_form, :html => {:class => "form-horizontal"}, url: candidates_send_email_path do |f| %>
<div class="form-group">
<%= f.label :template_name, "Template" %>
<div class="col-md-10">
<%= select_tag "template_name", options_from_collection_for_select(Email_Template.all, "id", "id") %>
</div>
</div>
<div class="form-group">
<%= f.label :template_body, "Template Body" %>
<div class="col-md-10">
<%= f.text_area :email_content, rows: 7 %><br/>
</div>
</div>
<div class=text-right>
<%= f.button class: "btn btn-primary btn-modal" do %>
SEND EMAIL <i class="glyphicon glyphicon-send"></i>
<% end %>
</div>
<% end %>
routes.rb
post '/template_body_finder/:template_id' => 'search#find_template'
search_controller.rb
def find_template
#email_template = Email_Template.find(params[:template_id])
respond_to do |format|
format.js
end
end
search.js.coffee
$ ->
$(document).on 'change', '#template_id', (evt) ->
template_id = $(this).val()
window.alert(template_id)
curr_url = "/template_body_finder/" + template_id
$.ajax({
type: 'POST',
url: curr_url,
data: {'template_id': template_id},
success: ( data, status, xhr ) -> })
find_template.js.erb
$(“#email_content”).val= <%= #email_template.template_body %>
;
Here try this:-
1- your form
<%= form_for :email_form, :html => {:class => "form-horizontal"}, url: candidates_send_email_path_path do |f| %>
<div class="form-group">
<%= f.label :template_name, "Template" %>
<div class="col-md-10">
<%= select_tag "template_name", options_from_collection_for_select(Email_Template.all, "id", "id") %>
</div>
</div>
<div class="form-group">
<%= f.label :template_body, "Template Body" %>
<div class="col-md-10">
<%= f.text_area :email_content, rows: 7 %><br/>
</div>
</div>
<div class=text-right>
<%= f.button class: "btn btn-primary btn-modal" do %>
SEND EMAIL <i class="glyphicon glyphicon-send"></i>
<% end %>
</div>
<% end %>
<script type="text/javascript">
$('#template_name').on('change', function(){
var template_id = $(this).val();
// alert(template_id);
// ajax request
$.ajax({
url: "/template_body_finder",
type: "GET",
data : {
template_id: template_id
},
dataType: "script",
});
})
</script>
2- change your url:
get '/template_body_finder' => 'search#find_template'
3- in controller -
def find_template
#email_template = Email_Template.find(params[:template_id])
respond_to do |format|
format.js
end
end
4- in your find_template.js.erb file it should like this-
$('#email_form_email_content').val("<%=#email_template.template_body%>");
this works for me, if you have any doubt you can ask in comments.
thank you.

How to store course other than dropdown menu and open new textbox put other educations?

Model field - degree, institute, percentage these are the model field.
If course is other than dropdown menu so select "Other" and display textbox for enter user education. and when form submit i want to store education in degree column. but now i am not able to handle these two value.
education.html.erb file
<p>
<%= f.label :user_id, "Select User.*" %>
<% if params[:user_id] != nil %>
<%= f.select :user_id, :collection => User.where(:id => params[:user_id]).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
<% else %>
<% #selected_user_id = EducationInformations.where(:id => params[:id]).pluck(:user_id).first %>
<%= f.select :user_id, :collection => User.where(:id => #selected_user_id).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
<% end %>
</p>
<br/>
<% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
<p>
<%= f.label :degree, "Select Course.*" %>
<%= f.select :degree , :collection => (course) %>
</p>
<div id="other_edu">
<br/>
<p>
<%= f.label :other_degree, "Enter your course" %>
<%= f.text_field :other_degree , :id => "other_course", :onblur => "getCourse()" %>
</p>
</div>
<br/>
<p>
<%= f.label :institute, "Institute*" %>
<%= f.text_field :institute %>
</p>
<br/>
<p>
<%= f.label :percentage, "Grade/Percentage" %>
<%= f.text_field :percentage %>
</p>
javascript
$(document).ready(function() {
var select = document.getElementById('education_informations_degree');
var other_course = document.getElementById('other_course');
$('#other_edu').hide();
select.onchange = function() {
var selected_course = select.value;
if (selected_course == "Other")
{
$('#other_edu').show();
}else{
$('#other_edu').hide();
}
}
});
function getCourse() {
var x = document.getElementById("other_course");
var select = document.getElementById('education_informations_degree');
select.value = x.value;
console.log(x.value);
}
Model
class EducationInformations < ActiveRecord::Base
unloadable
attr_accessible :user_id, :degree, :institute, :passing_year, :country, :university, :percentage
validates :degree, presence: true
validates :passing_year, presence: true
validates :university, presence: true
validates :institute, presence: true
end
Controller
def create
#edu_info = EducationInformations.new(educational_params)
if #edu_info.save
#flash[:notice] = 'Vote saved.'
redirect_to #edu_info
else
render "new"
end
end
private
def educational_params
params.require(:education_informations).permit(:user_id, :degree, :institute, :passing_year, :country, :university, :percentage)
end
How I fixed this problem when user select value from dropdown list so I set value as textbox value. If I select "Other" item so display textbox and whatever user can enter so I send value of text. I know this is not best way but I think this solution handle my problem.
View code
<% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
<p>
<%= label_tag "select_degree", "Select Course *" %>
<%= select_tag "select_degree" , options_for_select(course) %>
</p>
<div id="other_edu">
<br/>
<p>
<%= f.label :degree, "Enter your course *" %>
<%= f.text_field :degree %>
</p>
</div>
<br/>
<p>
<%= f.label :institute, "Institute *" %>
<%= f.text_field :institute %>
</p>
<br/>
<p>
<%= f.label :percentage, "Grade/Percentage " %>
<%= f.text_field :percentage %>
</p>
Javascript
$(document).ready(function() {
var select = document.getElementById('select_degree');
var other_course = document.getElementById('education_informations_degree');
$('#other_edu').hide();
select.onchange = function() {
var selected_course = select.value;
if (selected_course == "Other")
{
$('#other_edu').show();
}else{
$('#other_edu').hide();
other_course.value = select.value;
}
}
});

Issue with Payment method selection when I add Braintree Drop-in UI in Spree Store

I'm trying to implement extension to accommodate drop-in UI of braintree when customer selects Braintree as a payment method. If I add braintree js code into _gateway.html.erb then all the other payment methods stop working. If I select any other method except braintree and click "Save and continue" then nothing happens. "Save and continue" button just gets disabled.
I've overwritten spree/frontend/app/views/spree/checkout/_gateway.html.erb.
<% if payment_method.name == "Braintree" %>
<div id="dropin"></div>
<% else %>
<div class="well clearfix">
<%= image_tag 'credit_cards/credit_card.gif', :id => 'credit-card-image', :class => 'pull-right', :width => '170', :height => '28' %>
<% param_prefix = "payment_source[#{payment_method.id}]" %>
<p class="field">
<%= label_tag "name_on_card_#{payment_method.id}" do %>
<%= Spree.t(:name_on_card) %><abbr class="required" title="required">*</abbr>
<% end %>
<%= text_field_tag "#{param_prefix}[name]", "#{#order.billing_firstname} #{#order.billing_lastname}", { id: "name_on_card_#{payment_method.id}", :class => 'form-control required'} %>
</p>
<p class="field" data-hook="card_number">
<%= label_tag "card_number" do %>
<%= Spree.t(:card_number) %><abbr class="required" title="required">*</abbr>
<% end %>
<% options_hash = Rails.env.production? ? {:autocomplete => 'off'} : {} %>
<%= text_field_tag "#{param_prefix}[number]", '', options_hash.merge(:id => 'card_number', :class => 'form-control required cardNumber', :size => 19, :maxlength => 19, :autocomplete => "off") %>
<span id="card_type" style="display:none;">
( <span id="looks_like" ><%= Spree.t(:card_type_is) %> <span id="type"></span></span>
<span id="unrecognized"><%= Spree.t(:unrecognized_card_type) %></span>
)
</span>
</p>
<div class="row">
<div class="col-md-8 field" data-hook="card_expiration">
<%= label_tag "card_expiry" do %>
<%= Spree.t(:expiration) %><abbr class="required" title="required">*</abbr>
<% end %>
<%= text_field_tag "#{param_prefix}[expiry]", '', :id => 'card_expiry', :class => "form-control required cardExpiry", :placeholder => "MM / YY" %>
</div>
<div class="col-md-4 field" data-hook="card_code">
<%= label_tag "card_code" do %>
<%= Spree.t(:card_code) %><abbr class="required" title="required">*</abbr>
<% end %>
<%= text_field_tag "#{param_prefix}[verification_value]", '', options_hash.merge(:id => 'card_code', :class => 'form-control required cardCode', :size => 5) %>
<%= link_to "(#{Spree.t(:what_is_this)})", spree.content_path('cvv'), :target => '_blank', "data-hook" => "cvv_link", :id => "cvv_link" %>
</div>
</div>
<%= hidden_field_tag "#{param_prefix}[cc_type]", '', :id => "cc_type", :class => 'ccType' %>
</div>
<% end %>
<%= #client_token = Braintree::ClientToken.generate %>
<script type="text/javascript">
braintree.setup("<%=#client_token%>", 'dropin', {
container: 'dropin'
});
</script>
I work at Braintree. If you don't specify the form option inside the call to braintree.setup, braintree.js will attach its behavior to the closest parent form element. Since it looks like the parent form of your braintree container is the same form used by the other checkout flows, braintree.js will indeed hijack the call made by the submit button (regardless of the payment flow being used). I recommend creating two separate form elements, and passing the id of the one used by braintree to the braintree.setup call.
braintree.setup("<%=#client_token%>", 'dropin', {
container: ‘dropin’,
form: ‘braintree_checkout'
});

"First argument in form cannot contain nil or be empty" bug

Hello guys i would like someone to help me fix this bug.....i would like to sign up an account but i fail whenever i click register and i get the error above...Anyone who can help me fix this
please help me out i have tried and i can not understand why..
Below is my code for all the controllers responsible
code for the registrations controller
class RegistrationsController < Devise::RegistrationsController
#include Wicked::Wizard
#before_filter :configure_permitted_parameters, if: :devise_controller?
def new
super
end
def create
super
end
protected
def after_inactive_sign_up_path_for(resource)
user_steps_path
end
end
below is the code for the user_steps controller
class UserStepsController < ApplicationController
include Wicked::Wizard
steps :finishing_step
def show
#user = User.last
render_wizard
end
def update
render_wizard
end
private
def finish_wizard_path
current_user.update_attributes(complete: true)
redirect_to root_path
end
end
below is my code for the applications controller
class ApplicationController < ActionController::Base
layout :layout_by_resource
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, :alert => exception.message
end
before_filter do
resource = controller_name.singularize.to_sym
method = "#{resource}_params"
params[resource] &&= send(method) if respond_to?(method, true)
end
def after_sign_in_path_for(resource)
#this is where i decide that before user fills all fields he/she should be redirected to the user_steps_path
if resource.complete?
root_path
else
user_steps_path
end
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:first_name, :last_name, :phone_number,
:city, :nationality, :address_first_line, :address_second_line, :date_of_birth,
:email, :password, :password_confirmation, :terms_of_service )}
end
protected
def layout_by_resource
if devise_controller?
"login"
else
"application"
end
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :current_password, :is_female, :date_of_birth, :avatar) }
end
end
below is my code for the form that i want to submit to the user account after confirmation from the personal email
<%= form_for #user, url: wizard_path do |f| %>
<div><%= f.label :first_name, "First Name" %><br />
<%= f.text_field :first_name %></div>
<div><%= f.label :middle_name, "Middle Name" %><br />
<%= f.text_field :middle_name %></div>
<div><%= f.label :last_name, "Last Name" %><br />
<%= f.text_field :last_name %></div>
<div><%= f.label :phone_number, "Phone Number" %><br />
<%= f.text_field :phone_number %></div>
<div><%= f.label :date_of_birth, "Date of Birth" %><br />
<%= f.date_select :date_of_birth, start_year: 1900 %></div>
<div><%= f.label :address_first_line, "Address (first line)" %><br />
<%= f.text_field :address_first_line %></div>
<div><%= f.label :address_second_line, "Address (second line)" %><br />
<%= f.text_field :address_second_line %></div>
<div><%= f.label :city, "City" %><br />
<%= f.text_field :city %></div>
<div><%= f.label :nationality, "Nationality" %><br />
<%= country_select(:user, :nationality, {selected: "UG"}) %></div>
<div>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
</div>
<div>
<%= f.label :terms_of_service, "Agree to Terms of Service" %> <br>
<%= f.check_box :terms_of_service %>
</div>
<div><%= f.submit "Register" %></div>
<% end %>
For any help thanks in advance.....

form not displayed in page source

i am creating a veiw page in my rails application
this is my view page :
<html>
<head>
<%= stylesheet_link_tag "$.multiselect2side", :media => "all" %>
<%= javascript_include_tag "$.multiselect2side" %>
<script>
$().ready(function() {
$("#student_name_id").multiselect2side();
});
</script>
<script>
function callGrade(grade_value_123) {
var id = grade_value_123;
}
</script>>
</head>
<body>
<div class="container">
<h1> <%= #message%> </h1>
<%= select(:grade,:id,#distinct_grade_list,{},:onchange => "callGrade(this.value)")%>
<%= select(:period,:id,#distinct_period_list)%>
<%= select(:student_name,:id,#student_name_list,{},{ :multiple => true, :size => 4 }) %>
<br/>
<%= radio_button_tag(:age, :child,{},:onclick => "callMe(this.id)") %><%= label_tag(:age_child, :" General Curriculum") %>
<%= radio_button_tag(:age, :adult) %><%= label_tag(:age_adult, :"Common Core Standards") %>
<br/>
<div id="farzi" style="border: 3px solid red;margin-top: 20px">
<% form_tag('call_ajax', :method=>'post', :multipart => true) do %>
<p>
Subject:
<br />
<%= text_field_tag "subject" %>
</p>
<p>
Priority:
<br />
<%= select_tag("priority", options_for_select([['Critical', '1'],
['Important', '2'],['Standard','3']], '3')) %>
</p>
<p>
Describe your problem:
<br />
<%= text_area_tag "description", "", :size=>"50x20" %>
</p>
<p>
Add an attachment:
<br />
<%= file_field_tag "attachment" %>
</p>
<p>
<%= submit_tag 'Submit' %>
</p>
<% end %>
</div>
</body>
</html>
now when i execute the page , the page doent display any form ,
and when i check the page source , there is no form content inside the div with id= farzi
in the page source from browser it shows
<div id="farzi" style="border: 3px solid red;margin-top: 20px">
</div>
any explanation ??
Replace
<% form_tag('call_ajax', :method=>'post', :multipart => true) do %>
with this
<%= form_tag('call_ajax', :method=>'post', :multipart => true) do %>
The <%= is used to output data from ruby in html.erb. If you just execute first command, it will return the correct data, but it won't output anything in html.

Categories

Resources