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
Testlink
</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” %>’)
}
});
});
});
Why don’t you use remote_function() ?
Chirag Shah wrote in post #1013312:
Why don’t you use remote_function() ?
I used to do that but now I’m trying to do it with jquery.
For work with jquery and rails function you can use jrails plugin
Paul B. wrote in post #1013320:
Chirag Shah wrote in post #1013312:
Why don’t you use remote_function() ?
I used to do that but now I’m trying to do it with jquery.
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
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
Railscasts has some examples:
“Eric Björkvall” [email protected] 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.
Colin L. wrote in post #1017714:
On 21 August 2011 10:59, Paul B. [email protected] 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.
On 21 August 2011 15:16, Paul B. [email protected] 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.
Colin
On 21 August 2011 10:59, Paul B. [email protected] 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
Colin L. wrote in post #1017722:
On 21 August 2011 15:16, Paul B. [email protected] 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.
Colin
So how do I make a basic ajax call from a link, do something in an
action and update a div with new content?
@Paul B. :
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 B. [email protected]
On Aug 21, 8:03pm, Paul B. [email protected] wrote:
Colin L. 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
lionel first-developer wrote in post #1017744:
@Paul B. :
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.
lionel first-developer wrote in post #1017744:
@Paul B. :
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?
On 21 August 2011 21:11, Paul B. [email protected] wrote:
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
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 ?
This is what I’ve been doing for jquery:
layouts/application.html.erb:
<%= @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:
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.
Paul B. 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 B. did. I had read some other
things
about how to set rails to use jquery, but they seemed way too
complicated. I also found this:
http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/
and linking to google seems like the way to go.