rails newbie herem, I’m looking to learn a little bit about jquery so I figured I’d try to switch between my partial views using AJAX.
Right now on my user’s dash I have a link to their ‘likes’ page, it requires a full reload to see the ‘likes’ page, how would I change this to refresh with the likes_user_path@user)??
views/pages/home.html.erb
<div id="left">
<div id="dash-statistics">
<a href="<%= likes_user_path(@user) %>">
<div id="likes" class="stat">Likes
<%= @user.likes.count %>
</div>
</a>
</div>
</div>
<div id="right">
<div id="content">
</div>
</div>
UsersController
def likes
@title = "Likes"
@user = User.find(params[:id])
@like_stuff = @user.likes.paginate(:page => params[:page])
render 'show_likes'
end
What I understand is that you want to reload the
likesby clicking a link, by using ajax, in your user dashboard.First thing to do is rewrite the link, and add a
:remote => truelike this:Then, in your controller, make sure you have a
respond_to :html, :jsat the top (there can of course be other options, but:jshas to be among them)After that, you could do a function called
likesin your controller which to use for loading the likes, which looks something like yours, only you don’t dorenderat the end, butrespond_with @likes. By having:jsin therespond_tofilter, Rails will automagically act accordingly when supposed to.Next, make a corresponding view called
likes.js.erb(i’m not exactly sure if coffeescript will work out of the box in this case) in which you put something likeThis assumes that in your main view in which you want to render the likes ,there’s an element with the
id=likeswhich could look like this:and there’s also a
_likes.html.erbpartial which renders the likesAnd with this I think I pretty much covered it.