i18n is changing the output of Ajax call - ajax

I have a datepicker function that was working pefectly well until I internationalized my application. But now, it's not working anymore. Here is my Ajax function :
$datepicker.change(function(){
currentDate = $datepicker.datepicker( "getDate" );
dateString = $.datepicker.formatDate("yy-mm-dd", currentDate);
console.log("My dateString is: "+dateString);
Here my console is showing the right date
$.ajax({
type: "POST",
url: "movements/getTO/",
data: {"date":dateString},
}).done(function(data) {
$("#resultTO").html(data[0])
$("#resultQty").html(data[1])
});
});
with this controller function :
def getTO
selected_date = Date.parse(params[:date])
new_html_to_return1 = Movement.where(:movement_date =>selected_date, :user => current_user.email).sum("turnover")
new_html_to_return2 = Movement.where(:movement_date =>selected_date, :user => current_user.email).sum("quantity")
#table = [new_html_to_return1, new_html_to_return2]
render :json => #table
end
My routes.rb :
post "movements/getTO"
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
resources :movements
(...)
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
match '', to: redirect("/#{I18n.default_locale}")
At this point, my datepicker was having a route trouble. So I added in routes.rb
match 'movements/getTO', to: redirect("movements/getTO")
But that's not right because now I have as output : < and !. Incredible for sums isn't it ???

Related

Chartkick Datepicker/Range Selector - Possibility to choose dates to show data

I am using ahoy gem to track visits & chartkick gem to show the data to users.
In users dashboard page, I have around 3-4 charts and each of them show different data to user.
As of today, my charts only displays last 7 days & I do it by group_by_day(:started_at, last: 7).
= area_chart Visit.where(user: current_user).group_by_day(:started_at, last: 7).sum(:price), label: '$'
How can I add datepicker so users can select two dates (start & end date) and display data between selected/giving dates?
UPDATE:
I have created charts_controller.rb and get the charts from this controller & routes.
For instance:
class ChartsController < ApplicationController
def visits
result = Visit.where(user: current_user).group_by_day(:started_at, last: 7).count
render json: [{name: 'Visits', data: result}]
end
My routes.rb:
resources :charts, only: [] do
collection do
get 'visits'
end
end
My view:
= area_chart visits_charts_path
You can create a datepicker and add an onchange listenner on it, then you send the start and end date using ajax to a controller that renders your chart:
$(".end_at").change(function(e){
end_at = $(e.target).val();
start_at = $(".start_at").val();
$.ajax({
type: "POST",
url: "/charts",
data: {
start_at: start_at,
end_at: end_at,
},
success: function (data) {
$(".chart-container").html(data);
},
});
});
In your controller :
class ChartsController < ApplicationController
def visits
start_at = params[:start_at].to_date
end_at = params[:end_at].to_date
render :inline => "<%= area_chart Visit.where(user: current_user).group_by_day(:started_at, range: start_at..end_at).sum(:price), label: '$' %>"
end
end

ruby, sinatra, Plivo API: render API call as HTML

I am making an API call to Plivo to list available telephone numbers.
I can access the returned response and print the desired elements in my terminal BUT I do not know how to render them as HTML on my web page. This is my problem.
In the terminal, the response to a successful call is:
{"api_id"=>"23f1f0f0-0808-11e3-a442-22000ac6194a",
"meta"=>
{"limit"=>1, "next"=>nil, "offset"=>0, "previous"=>nil, "total_count"=>1},
"objects"=>
[{"group_id"=>"23928520636825",
"number_type"=>"local",
"prefix"=>"646",
"region"=>"New York, UNITED STATES",
"rental_rate"=>"0.80000",
"resource_uri"=>
"/v1/Account/MAZDQ1ZJIYMDZKMMZKYM/AvailableNumberGroup/23928520636825/",
"setup_rate"=>"0.00000",
"sms_enabled"=>true,
"sms_rate"=>"0.00800",
"stock"=>50,
"voice_enabled"=>true,
"voice_rate"=>"0.00900"}]}
"0.00900"
New York, UNITED STATES
646
The Ajax script which generates the response is:
$(".localsearch").click(function() {
var country_iso = $("#local").val();
var region = $("#region").val();
var prefix = $("#prefix").val();
$.ajax({
type: "GET",
url: "/local/data",
data: { 'country_iso' : country_iso, 'region' : region, 'prefix' : prefix },
success: function(data) {
alert(data)
},
});
});
The alert doesn't help and just shows the entire page.
The ruby code is:
get '/local/data' do
country_iso = params[:country_iso]
region = params[:region]
prefix = params[:prefix]
p = RestAPI.new(AUTH_ID, AUTH_TOKEN)
params = {'country_iso' => country_iso, 'region' => region, 'prefix' => prefix, 'limit' => '1'}
response = p.get_number_group(params)
obj = response.last
pp response.last
#region = obj["objects"][0]["region"]
puts #region
#prefix = obj["objects"][0]["prefix"]
puts #prefix
erb :search
end
So, sorry it's long and to summarize, how do I extract elements from the API response and print them as HTML? Many thanks in advance.
In the view I have tried:
<%= #region %> and <%= obj['region'] %> and <%= obj['objects][0]['region'] %>and none of them work.
Yours is a perfect use case of of rendering a partial through a ajax call.
So what you can do is:
Make your Sinatra action return html using rails like render partial functionality like this
http://steve.dynedge.co.uk/2010/04/14/render-rails-style-partials-in-sinatra/
(to get rails like partial functionality in sinatra you can use this gem also https://rubygems.org/gems/sinatra-partial )
Now since now your sinatra action returns a valid html, in your ajax success function you can just write:
$(".localsearch").click(function() {
var country_iso = $("#local").val();
var region = $("#region").val();
var prefix = $("#prefix").val();
$.ajax({
type: "GET",
url: "/local/data",
data: { 'country_iso' : country_iso, 'region' : region, 'prefix' : prefix },
success: function(data) {
$('unique_identifier_of_your_partial_on_the_html_dom').html(response)
}
});
});
another example of rendering partial in sinatra:
Ruby w/ Sinatra: Could I have an example of a jQuery AJAX request?
extract out the html that you want to populate with the response from this ajax call into a a separate erb file lets say , _my_response_partial.html.erb
now suppose this is your search.html.erb file.
#something somrthing
<%= erb(:my_response_partial, locals => {:region => #region, :prefix => #prefix},:layout => false) %> #pass whatever data you want to pass to a partial using locales
# something something
and in your get action replace the last line with:
erb(:my_response_partial, locals => {:region => #region, :prefix => #prefix},:layout => false)
By this way your action will just return the html required to populate that partial.

ASP MVC3 and Telerik DatePicker control blocking year/month pick after Ajax action

So I'm in big trouble here.
<%: Html.Telerik().DatePicker().Name("dataWprowadzeniaOd").ShowButton(true)%> -
<%:Html.Telerik().DatePicker().Name("dataWprowadzeniaDo").ShowButton(true) %>
I have these two nice DatePickers, a list and some filtering options, all on Ajax. Now when Ajax action occurs (like filtering, refreshing table, changing pages) these two cute little things block in a way user is not able to change month and year there.
Here's beginning of table code:
Html.Telerik().Grid(Model)
.Name("Main")
.DataKeys(keys => keys.Add(p => p.DepozytID))
.Localizable("pl-PL")
.Pageable(paging => paging.Enabled(true).PageSize(20))
I've tried putting:
$('#dataWprowadzeniaOd').tDatePicker({ format: 'yyyy-MM-dd', minValue: new Date(1899, 11, 31),
maxValue: new Date(2100, 0, 1) });
$('#dataWprowadzeniaDo').tDatePicker({ format: 'yyyy-MM-dd', minValue: new Date(1899, 11, 31),
maxValue: new Date(2100, 0, 1) });
at the end of every callback/ajax function, but it removes month/year bar from calendar so I can't check if it works o.O
Tried this:
$('#dataWprowadzeniaOd').attr('disabled', 'disabled');
$('#dataWprowadzeniaOd').attr('disabled', '');
too, but no effect.
function filtruj() {
var newurl = '<%: Url.Content("~/RejestrDepozytow/ListaDepozytow") %>';
var filtr = {};
filtr.typStatusu = $("#typStatusu").val();
filtr.rodzajDepozytuID = $("#rodzajDepozytuID").val();
filtr.dataWprowadzeniaOd = $("#dataWprowadzeniaOd").val();
filtr.dataWprowadzeniaDo = $("#dataWprowadzeniaDo").val();
filtr.podmiotSkladajacyID = $("#podmiotSkladajacyID").val();
filtr.podmiot = $("#podmiot").val();
filtr.sygnaturaSprawy = $("#sygnaturaSprawy").val();
filtr.barcode = $("#barcode").val();
filtr.numerLp = $("#numerLp").val();
filtr.numerRok = $("#numerRok").val();
$.ajax({
type: "POST",
async: false,
url: newurl,
data: filtr,
success: function (dane) {
$("#gridDepozytow").html(dane);
}
});
}
Here's method used just before datepicker getting blocked.

RoR - Retrieve a data from controller and display using AJAX

I have a controller that assign to pass a value to ajax but when in execution no value are printed, here are my code:
:User Controller
def search_name
#nama = params[:name] //Get a value from user input
#user = User.select(:name).where(['name = ?', params[#nama]]).all
respond_to do |format|
format.json {render :json => #user}
format.html { render :json => #user}
end
end
:my.js
$.ajax({
url: '/users/search_name/',
type:'get',
data:{
name:nama
},
dataType:'json',
success:function(puser){
var userName = '';
for(x in puser){
userName += 'Name here"'+ puser[x]['user']['name'] +'";
}
$('.search-result').html(userName);
}
})
to debug this js issue, please use your browser to visit: /users/search_name , to see what the result is.

Grails redirect not working

ismy controller does not work and I don't understand why here is it:
class FormmakerController {
def fileManipulatorService
def index = { }
def idProcessor = {
String idToProcess = params.urlEncParam
//Params lookalike urlEncParam:301-303-304-305
if(idToProcess != null){
String globalForm = ''
idToProcess.split('-').each {item->
globalForm += fileManipulatorService.fileProvider(item).getText()
}
//render(text: globalForm, contentType: "text/xml", encoding: "ISO-8859-1")
//response.sendError(200)
redirect(controller: 'tools', action: 'index', params: [globalForm: String])
}}}
Note : the controller is called from an ajax request and my redirect instruction need to call an action in a different controller with Text params.
Thanks for your help.
The params on your redirect looks incorrect. Try:
redirect(controller: 'tools', action: 'index', params: [globalForm: globalForm])

Resources