paramater passing with ajax in rails

31 views
Skip to first unread message

amvis

unread,
May 23, 2012, 2:18:18 AM5/23/12
to rubyonra...@googlegroups.com
view.html 
  
<%= submit_tag "act",:id => params[:id], :class => "actcpn btn btn-success" %>

.js

$(".actcpn").click(function() {
$.ajax({
type: "PUT",
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

azizmb.in

unread,
May 23, 2012, 2:32:59 AM5/23/12
to rubyonra...@googlegroups.com
Hey!

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


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2gzWsZv8J0UJ.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.



--
- Aziz M. Bookwala

amvis

unread,
May 23, 2012, 3:16:43 AM5/23/12
to rubyonra...@googlegroups.com


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",
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..?











 






On Wed, May 23, 2012 at 11:48 AM, amvis <vgrkr...@gmail.com> wrote:
view.html 
  
<%= submit_tag "act",:id => params[:id], :class => "actcpn btn btn-success" %>

.js

$(".actcpn").click(function() {
$.ajax({
type: "PUT",
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2gzWsZv8J0UJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Robert Walker

unread,
May 24, 2012, 8:12:15 PM5/24/12
to rubyonra...@googlegroups.com
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.

--
Posted via http://www.ruby-forum.com/.

Robert Walker

unread,
May 24, 2012, 8:22:48 PM5/24/12
to rubyonra...@googlegroups.com
Robert Walker 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.
Reply all
Reply to author
Forward
0 new messages