Paramater passing with ajax in rails

view.html

<%= submit_tag “act”,:id => params[:id], :class => “actcpn btn
btn-success” %>

.js
*
*
$(“.actcpn”).click(function() {
$.ajax({
type: “PUT”,
url: “http://localhost:3000/pgm/activate/”,
success: function(){
$(‘.nplabel’).fadeIn(300).show();
$(‘.nplabel’).fadeOut(3000);
}
});
});

class PgmControlller
def activate

    • @coupon_id = params[:id]
      end
      Here the problem is i need to get the params[:id] in controller. so when
      the act button click in view, that will call the ajax through class .*
      actcpn,* where i give the url to the controller,How to pass that button
      id
      via that ajax url to controller?

Thanks
vishnu

On Wednesday, 23 May 2012 02:32:59 UTC-4, azizmb.in wrote:

Hey!

Can you be a little clearer about what you are trying to accomplish?

yes, in my project, the view file is pgm.html.erb, that have one
button

pgm.html.erb
*
*
*
*
<%= submit_tag “activate”,:id => params[:id], :class =>
actcpn btn
btn-success” %>

Here the controller

  • *class PgmsController

    def cpn_activate
    @cpn_id = params[:id]

  •   puts "b_id#{@cpn_id}"*
    

    end

end

header.js
*
*
$(“.actcpn”).click(function() {
$.ajax({
type: “PUT”,
url: “http://localhost:3000/pgms/cpn_activate/”,
success: function(){
$(‘.nplabel’).fadeIn(300).show();
$(‘.nplabel’).fadeOut(3000);
}
});
});

i have one button in pgm.html.erb, that button name is activate, so
when
i click on that button, that will call one ajax with the class id
(actcpn),
in that ajax function, i have specified the url: *
http://localhost:3000/pgms/cpn_activate/”* to controller, In that
controller i have to get the button id, which i pass :id =>
params[:id]
,
through button…

So how to pass that button ID via the url from the ajax…?

amvis wrote in post #1061789:

On Wednesday, 23 May 2012 02:32:59 UTC-4, azizmb.in wrote:
header.js
*
*
$(“.actcpn”).click(function() {
$.ajax({
type: “PUT”,
url: “http://localhost:3000/pgms/cpn_activate/”,
success: function(){
$(‘.nplabel’).fadeIn(300).show();
$(‘.nplabel’).fadeOut(3000);
}
});
});

So how to pass that button ID via the url from the ajax…?

You pass the parameter the same way you always do with HTTP. Put it in
the query string or request body. JQuery makes this easy using the
“data” attribute:

$.ajax({
type: ‘PUT’,
url: url,
data: data,
success: success,
dataType: dataType
});

data - A map or string that is sent to the server with the request.

Robert W. wrote in post #1062048:

You pass the parameter the same way you always do with HTTP. Put it in
the query string or request body. JQuery makes this easy using the
“data” attribute:

$.ajax({
type: ‘PUT’,
url: url,
data: data,
success: success,
dataType: dataType
});

data - A map or string that is sent to the server with the request.

I forgot one other possible location. The data/attribute could be part
of the URL itself, as is often the case with Rails routes (i.e.
http://localhost:3000/pgms/cpn_activate/1 #where 1 is the id).

P.S. That’s a pretty ugly URL for a Rails app by the way. Maybe you
should consider cleaning that up. Start with not abbreviating so much.
If “pgms” means “programs” then have a Program model and
“/programs/:action/:id” for the route. Search engines will also
appreciate having actual words in the URL. The more clear the intent the
better. We’re not in danger of running out of letters on the web.

Hey!

Can you be a little clearer about what you are trying to accomplish?

On Wed, May 23, 2012 at 11:48 AM, amvis [email protected] wrote:

type: “PUT”,

  • Aziz M. Bookwala

Website http://azizmb.in/ | Twitter https://twitter.com/azizbookwala
|
Github http://github.com/azizmb