Rails 3 - Ajax helpers - ajax

I am trying to submit a form remotely.
<% form_for #awareness_item, :remote => 'true', :url => admin_awareness_item_path(#awareness_item), :html => {:id => 'updteAwarenessItem'} do |f| %>
and have the below ajax callbacks configured to the form,
$(document).ready(function(){
$("#updteAwarenessItem").unbind();
$("#updteAwarenessItem")
.bind("ajax:success", function(event, data, status, xhr) {
console.log('...POSTING....');
})
.bind("ajax:loading", function(event, data, status, xhr) {
console.log('...LOADING....');
});
});
The problem is, that though I have bound the ajax:loading callback with the form, it never gets called. The onlything that ever gets called is ajax:success. Kindly let me know how can I bind ajax:loading and ajax:complete to the form. Am I doing something wrong here?

Some callbacks have been renamed (see the pull request).
ajax:loading is now ajax:beforeSend, because it's been named beforeSend in jQuery, to avoid confusion.

Related

Don't wait for form submit redirect

i'm trying to test an application using cypress. I have an odd scenario where i have an html form with an action tag, some fields in the form and then a button for submit it.
I'm filling out all the fields in the form and click the button for submission, but i'm getting this error:
I don't wanna wait for the response, cause in this odd case the response is redirecting for a page i don't have access and the test fails, i'm just want a check the form submission response by checking something simple, for instance the response status.
Any help will be welcome...
Thanks in advance..
Can you try the below cy.request() method. I don't know if there is any authorization needed for your request. Give a try as below and let me know.
it('Check response status', function () {
cy.request({
method: 'post',
url: 'url_here',
form: true,
headers: {
accept: "application/json",
},
}).then((response) => {
expect(response.status).to.eq(200);
})
})

Call different action on checking checkbox than on unchecking

I'd like to call one controller action on checking checkbox and another on unchecking.
That is, I want to call "meeting#create" on checking and "meeting#destroy" on unchecking.
This is signup form where You can sign up for several meetings via checkboxes. On checking the checkbox, the corresponding meeting gets added to an order (practically a cart) and on unchecking, the corresponding meeting gets removed from the order.
How is it possible to call a different action depending on actual status of the check_box_tag?
I'm no professional, so it took me long enough to figure out the options I should supply (the url_for) and I don't have a clue how to do it conditionally.
Please help!
<% #meeting.each do |meeting| %>
<%= check_box_tag 'checkbox', meeting.id, checked = false, :data => {:remote => true, url: url_for(controller: :order, action: :create, meeting_id: meeting), method: :post}, :class => 'checkbox' %>
<% end %>
In the end, I wrote the ajax script by hand.
$( document ).ready(function() {
$(".checkbox").click(function() {
var chb = $(this);
if($(this).prop('checked')){
$.ajax({
url: '/bookings?meeting_id='+chb.attr('value'),
type: "post"
});
}
else{
$.ajax({
url: '/bookings?meeting_id='+chb.attr('value')+'&cart_id='+currentcartid,
type: "delete"
});
};
});
});
One ajax script sufficed.

How to render json and at the same time refresh a partial using AJAX (Rails)

I have a controller action (lets call it some_controller_action) that needs to render out some json (for the javascript to pick up) and at the same time refresh/re-render a partial using some_controller_action.js.erb.
My code suggestion is something like this:
def some_controller_action
#
# lots of code here
#
respond_to do |format|
render :json => {:something => something}, :status => :ok
format.html {
redirect_to somewhere
}
format.js
end
end
The client side ajax/javascript triggers this action and receives the json as response. It looks like this:
someCoffeescripAction = ->
$.ajax
type: "post"
dataType: "json"
data:
some_data_name: some_data_content
url: "some_url/some_controller_action"
success: (data, status, xhr) ->
do_something_with_the_response(data)
error: (xhr, textStatus, errorThrown) ->
Of course this don't work, as I am not allowed to render twice in the same action. But then what to do?
What does your javascript looks like? Do you fetch the controller action by ajax? If so, then you just have to wait until the ajax call is done, and then trigger a re-render of your partial, either by using the just fetched json data, or by calling another ajax function.

MVCContrib Not Refresh JQuery Ajax

Hi I'm trying to refresh a mvccontrib grid, using ajax in MVC but it doesn't work. When I use "return PartialView("myview", mylist);" nothing happend. The response never came to the jquery code. I put the keyword debbuger to see if it works but never got there.
this is my JQuery Code to refresh the grid:
function refreshTable() {
$.ajaxSetup({
async: false,
cache: false
});
$.get('<%= Url.Action("RefreshGridData", "Countries") %>',
function (response) {
debugger;
$(".table-list").replaceWith(response)
});
}
And this is my Action:
public ActionResult RefreshGridData()
{
GetAllCountries();//this puts a list in the ViewData
return PartialView("CountriesPage", (List<iCatalogData.Country>)ViewData["CountriesList"]);
}
And this is my grid:
<div id="container">
<% Html.Grid((List<iCatalogData.Country>)ViewData["CountriesList"])
.Columns(column =>
{
column.For(co => Html.ActionLink(co.IdCountry.ToString(), "EditCountry", "Countries", new { id = co.IdCountry }, null)).Named("Id Country");
column.For(co => co.CountryName);
column.For(co => Html.ActionLink("Delete", "DeleteCountry", "Countries", new { id = co.IdCountry }, null)).Named("Delete");
}).Attributes(id => "example", #class => "table-list", style => "width: 100%;").Empty("No countries available").Render();
%>
</div>
Thanks.
I had a similar problem and solved it by changing the return type of the action to PartialViewResult instead of ActionResult. Could possibly be the same issue for you.

calling ruby with ajax

I'm wanting to check a ruby session variable via ajax. I'm familiar with ajax calling php, is calling a ruby file similar?
I'm talking about a 'session[:var_name]' type variable in a Rails environment using JQuery.
Something like:
$.ajax({
url: path to ruby file,
type: "GET",
dataType: "html",
success: function(html){
...
AJAX requests are not that different from other requests. You might need to change your return type based on what kind of AJAX request you make. If your Javascript is expecting text you can just write an action in a controller like this:
def your_ajax_action
render :text => session[:var_name].to_s
end
If your Javascript is expecting XML, you need to generate the XML then have an action in your controller like this:
def your_ajax_action
render :xml => {:var_name => session[:var_name]}.to_xml
end
Or, if you expect JSON:
def your_ajax_action
render :json => {:var_name => session[:var_name]}.to_json
end

Resources