How do I make an ajax call and then update a div with a partial? I''ve
tried this but it''s not working. Comment is created but the partial is
not loaded.
//view
<a href="#" id="testlink">Testlink</a>
<div id="commentlist">
</div>
//controller
def new
@comment = Comment.new
@comment.save
respond_to do |format|
format.html { }
format.js { }
end
end
//js
$(function() {
$(''#testlink'').click(function() {
$.ajax({
type: "GET",
url: "/comments/new",
success: function(){
$(''#commentlist'').html(''<%= render
:partial => "comments" %>'')
}
});
});
});
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Why don''t you use remote_function() ? - Chirag Shah -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Chirag Shah wrote in post #1013312:> Why don''t you use remote_function() ? > > - Chirag ShahI used to do that but now I''m trying to do it with jquery. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
For work with jquery and rails function you can use jrails plugin - Chirag Shah Paul Bergstrom wrote in post #1013320:> Chirag Shah wrote in post #1013312: >> Why don''t you use remote_function() ? >> >> - Chirag Shah > > I used to do that but now I''m trying to do it with jquery.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
One way:
link_to "#", new_comment_path, :remote => true, :id =>
"new-comment-link"
new.js.erb
$(#new-comment-link).hide();
$(''#commentlist'').html(''<%= render :partial =>
"comments" %>'')
You can use ruby logic in the .js.erb file - just like you could in former .rjs
files.
<% if @condition %>
alert("condition <%= @condition %>");
<% end %>
Cheers,
Eric
On 27 jul 2011, at 13:45, Paul Bergstrom wrote:
> How do I make an ajax call and then update a div with a partial?
I''ve
> tried this but it''s not working. Comment is created but the
partial is
> not loaded.
>
> //view
> <a href="#" id="testlink">Testlink</a>
> <div id="commentlist">
>
> </div>
>
> //controller
> def new
>
> @comment = Comment.new
> @comment.save
>
> respond_to do |format|
> format.html { }
> format.js { }
> end
>
> end
>
> //js
> $(function() {
>
> $(''#testlink'').click(function() {
> $.ajax({
> type: "GET",
> url: "/comments/new",
> success: function(){
> $(''#commentlist'').html(''<%= render
:partial => "comments" %>'')
>
> }
> });
> });
> });
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Railscasts has some examples: http://railscasts.com/episodes/43-ajax-with-rjs http://railscasts.com/episodes/136-jquery On 27 jul, 08:45, Paul Bergstrom <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> How do I make an ajax call and then update a div with a partial? I''ve > tried this but it''s not working. Comment is created but the partial is > not loaded. > > //view > <a href="#" id="testlink">Testlink</a> > <div id="commentlist"> > > </div> > > //controller > def new > > @comment = Comment.new > @comment.save > > respond_to do |format| > format.html { } > format.js { } > end > > end > > //js > $(function() { > > $(''#testlink'').click(function() { > $.ajax({ > type: "GET", > url: "/comments/new", > success: function(){ > $(''#commentlist'').html(''<%= render :partial => "comments" %>'') > > } > }); > }); > > }); > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
One way:
link_to "#", new_comment_path, :remote => true, :id =>
"new-comment-link"
new.js.erb
$(#new-comment-link).hide();
$(''#commentlist'').html(''<%= render :partial =>
"comments" %>'')
You can use ruby logic in the .js.erb file - just like you could in
former .rjs files.
<% if @condition %>
alert("condition <%= @condition %>");
<% end %>
Cheers,
Eric
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
"Eric Björkvall" <eric.bjorkvall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote in post #1013626:> One way: > > link_to "#", new_comment_path, :remote => true, :id => > "new-comment-link" > > new.js.erb > > $(#new-comment-link).hide(); > $(''#commentlist'').html(''<%= render :partial => "comments" %>'') > > > You can use ruby logic in the .js.erb file - just like you could in > former .rjs files. > > <% if @condition %> > alert("condition <%= @condition %>"); > <% end %> > > > > Cheers, > EricThanks. But are you sure this works? Doesn''t work here. If I have <%= link_to ''Test'', { :action => ''test'' }, :remote => true %> it actually goes to the action not making an ajax call. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 21 August 2011 10:59, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> "Eric Björkvall" <eric.bjorkvall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote in post #1013626: >> One way: >> >> link_to "#", new_comment_path, :remote => true, :id => >> "new-comment-link" >> >> new.js.erb >> >> $(#new-comment-link).hide(); >> $(''#commentlist'').html(''<%= render :partial => "comments" %>'') >> >> >> You can use ruby logic in the .js.erb file - just like you could in >> former .rjs files. >> >> <% if @condition %> >> alert("condition <%= @condition %>"); >> <% end %> >> >> >> >> Cheers, >> Eric > > Thanks. But are you sure this works? Doesn''t work here. > > If I have <%= link_to ''Test'', { :action => ''test'' }, :remote => true %> > it actually goes to the action not making an ajax call.What do you mean by that? It *should* go to the action, as an ajax call. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
@Paul Bergstrom :
In rails 3, handleling an Ajax request is too easy with Ujs. I''ll try
to
give you a good example.
I suppose, i want to ''activate'' post an article via an Ajax
call :
I have :
- posts_controller
- Route match for activating in Routes.rb file
- Views/posts/ directory where views related t o ''posts''
are located.
- And of course a model (optional for our case) :-)
- Suppose that i have a confirm link that''s alow me to
''activate'' a
particular ''post''.
<%= link_to ''activate'' , {:controller =>
"posts", :action => "activate",
:id => post.id}, :remote => true %>
- I also have in Routes.rb file this satement :
match "posts/activate/:id" => "posts#activate"
- I have in the Posts_controller file the action ''activate'' :
def activate
respond_to do |format|
format.js { }
end
end
- And finally, you have to create a file named
''activate.js.erb'' and put
this test line of code for ajax responding :
alert("it''s work :D");
Just try this for your case an keep me update.
Good luck
2011/8/21 Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
> "Eric Björkvall"
<eric.bjorkvall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote in post
#1013626:
> > One way:
> >
> > link_to "#", new_comment_path, :remote => true, :id =>
> > "new-comment-link"
> >
> > new.js.erb
> >
> > $(#new-comment-link).hide();
> > $(''#commentlist'').html(''<%= render
:partial => "comments" %>'')
> >
> >
> > You can use ruby logic in the .js.erb file - just like you could in
> > former .rjs files.
> >
> > <% if @condition %>
> > alert("condition <%= @condition %>");
> > <% end %>
> >
> >
> >
> > Cheers,
> > Eric
>
> Thanks. But are you sure this works? Doesn''t work here.
>
> If I have <%= link_to ''Test'', { :action =>
''test'' }, :remote => true %>
> it actually goes to the action not making an ajax call.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
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.
Colin Law wrote in post #1017714:> On 21 August 2011 10:59, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> >>> Cheers, >>> Eric >> >> Thanks. But are you sure this works? Doesn''t work here. >> >> If I have <%= link_to ''Test'', { :action => ''test'' }, :remote => true %> >> it actually goes to the action not making an ajax call. > > What do you mean by that? It *should* go to the action, as an ajax > call. > > ColinIt goes to the "page", the url. With ajax it should stay on the same page, same view, but just execute the js. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 August 2011 15:16, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1017714: >> On 21 August 2011 10:59, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> >>>> Cheers, >>>> Eric >>> >>> Thanks. But are you sure this works? Doesn''t work here. >>> >>> If I have <%= link_to ''Test'', { :action => ''test'' }, :remote => true %> >>> it actually goes to the action not making an ajax call. >> >> What do you mean by that? It *should* go to the action, as an ajax >> call. >> >> Colin > > It goes to the "page", the url. With ajax it should stay on the same > page, same view, but just execute the js.It should go to the ''test'' action. What happens then depends partly on what you do in that action. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Colin Law wrote in post #1017722:> On 21 August 2011 15:16, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> >>> What do you mean by that? It *should* go to the action, as an ajax >>> call. >>> >>> Colin >> >> It goes to the "page", the url. With ajax it should stay on the same >> page, same view, but just execute the js. > > It should go to the ''test'' action. What happens then depends partly > on what you do in that action. > > ColinSo how do I make a basic ajax call from a link, do something in an action and update a div with new content? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 21, 8:03 pm, Paul Bergstrom <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1017722:> So how do I make a basic ajax call from a link, do something in an > action and update a div with new content?If you want to follow the jqueryish way, then you setup a click handler on the link as your first post does, but your success function should be more along the lines of function(data){ $(''#commentlist'').html(data) } (which assumes that your action is producing a chunk of html in response) Fred> > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
lionel first-developer wrote in post #1017744:> @Paul Bergstrom :> > Just try this for your case an keep me update. > > Good luckThanks for the help. I tried it. It makes a http request and goes to the url. No alert message. Strange. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 August 2011 21:11, Paul Bergstrom <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> lionel first-developer wrote in post #1017744: >> @Paul Bergstrom : > >> >> Just try this for your case an keep me update. >> >> Good luck > > Thanks for the help. > > I tried it. It makes a http request and goes to the url. No alert > message. Strange.Try using Firebug in Firefox to check there are no script errors. Then copy the complete html source from the page (View > Page Source or similar in browser) and paste it into the w3c html validator to check the html is ok. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
lionel first-developer wrote in post #1017744:> @Paul Bergstrom : > > In rails 3, handleling an Ajax request is too easy with Ujs. I''ll try toBtw, are you sure I don''t need an extra gem for UJS? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is what I''ve been doing for jquery:
layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<title><%= @title %></title>
<%= csrf_meta_tag %>
<% javascript_include_tag(
"http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js",
"jquery.rails.js"
)
%>
Then go here:
https://github.com/rails/jquery-ujs
and click on:
src/
then click on:
rails.js
Copy that whole file and paste it into a file that you name:
jquery.rails.js
and place that file in the directory:
public/javascripts
You should see rails.js in that directory, which is for prototype. Note
how javascript_include_tag() links to jquery.rails.js.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
yes do you have this in your Gemfile : gem ''jquery-rails'' ? and also have you run this command : '' rails g jquery:install '' , to install jquery require file ? On 21 août, 22:27, Paul Bergstrom <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> lionel first-developer wrote in post #1017744: > > > @Paul Bergstrom : > > > In rails 3, handleling an Ajax request is too easy with Ujs. I''ll try to > > Btw, are you sure I don''t need an extra gem for UJS? > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
7stud -- wrote in post #1017753:> This is what I''ve been doing for jquery:> jquery.rails.jsThank you. Now it works. :-) So I''ve been sitting with this problem for a long time and this was what I missed. Did I miss it or was it poorly explained? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Paul Bergstrom wrote in post #1017792:> 7stud -- wrote in post #1017753: >> This is what I''ve been doing for jquery: > >> jquery.rails.js > > > Thank you. Now it works. :-) > > So I''ve been sitting with this problem for a long time and this was what > I missed. Did I miss it or was it poorly explained? >I don''t know what you read? I listened to a railscast on unobtrusive javascript, and that is what Ryan Bates did. I read some other things about setting jquery in rails, too, and they seemed way too complicated. Then I found this: http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/ and it seems like the way to go. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.