Wednesday 15 September 2010

javascript - AJAX call to render partial in Rails 3 -


I have a user's dashboard page where there is a sidebar on the left side such as projects, blogs etc. I would like to be able to click on a link like "Projects" link and then in the main area, the view for projects (see the bullet list of projects) will be loaded without refreshing the page. I will do it with AJAX I am trying, but things are not working.

Here I think it's going to work. User show.html.erb

  , '/ dash', remote: true%> Clicks on the sidebar link in   

Where / dash is configured for users # Show config / routes.rb is as follows:

  Match '/ Dash' = & gt; 'User # Show'   

And then, the show operation is called in the users_controller.rb controller: < Pre> Def Show @ User = User.Fund (Params [: ID]) @ Projects = @ User Points. Format Format.html # renders show.html.erb format.js # renders show.js.erb End End

Where show.js.erb is executed . This is a line in my show.js.erb file:

  $ ('# ajax'). Html ("& lt;% = escape_javascript (render (: partial = & gt; project / index ')). Html_safe% & gt;");   

And this is my show.html. ARB :

  & lt; #Ajax div is to be modified in div id = "Ajax" & gt; & Lt;% = Render: Template = & gt; 'Project / Index'% & gt; & Lt; / Div & gt;   

Apps / Ideas / Project / Index.html. RB takes in projects and gives a list, such as:

  <% @ projects.each do | Project | & Gt%; & Lt; P & gt; & Lt;% = project.name% & gt; & Lt; / P & gt; & Lt;% end% & gt;   

Obviously, I am doing the wrong thing because it is not working. What am I doing wrong? Do I need to change some more code? I am at Rail 3.2.13, so under // = jquery and // = jquery_ujs under app / assets / javascripts / application.js Must Import Required jQuery and AJAX Functionality

The problem is that your Show.js.erb file is partly But you are giving it a file which is not partial, partial files start with underscore _ .

Therefore, to avoid duplicate code, here is what I do:

  # app / visual /projects/show.js.erb $ ('# Ajax '). Html ("& lt;% = escape_javascript (render: partial = & gt; 'index',: local = & gt; {: projects = & gt; @projects})% & gt;"); #app / ideas / project / index.html ARB & lt;% = Render: Partial = & gt; 'Index',: local = & gt; {: Projects = & gt; @ Project}%> #Applications/Project/ ** _ ** index.html.erb # Include code that you did earlier in index.html.erb # Do not forget to change projects @ projects; projects.each; Do Project | & Gt%; & Lt; P & gt; & Lt;% = project.name% & gt; & Lt; / P & gt; & Lt;% end% & gt;   

In addition, you should not use matches in your own routes, because you will never need your routes too much, both GET and POST Should be accessible from the methods. Then try using get instead of mail .

No comments:

Post a Comment