In rails 3.1, I implement as:
1. Define test action in post controller:
def test
@p = Post.first
respond_to do |format|
format.js
end
end
2. Define in routes.rb:
resources :posts do
collection do
get 'test'
end
end
3. In index.html.erb of post folder, I add link:
<%= link_to "test", test_post_path, :remote => true %>
4. Create test.js.erb file in post folder:
code jQuery
My question is: “Is my implement Ajax in rails 3.1?” I don’t really understand about ajax in rails.
If no, can you give me a link about ajax in rails 3.1 and the technique I set up, what is it?
Yes it is ajax. When you add
:remote => trueto links and forms in rails, it automatically sends data to the server without refreshing your page on the browser. That’s how ajax works in every language/script.In your case, you can put js code in your test.js.erb file to update/interact the page (with jQuery). You might want to update some html div elements with the attributes of instance variable you set up
@p.Eg. in your test.js.erb
Then you have essentially made an ajax request to the server and update the client browser page with the server data without refreshing.