轨道3:jQuery来改变谐音 [英] Rails 3: jquery to change partials
问题描述
轨道新手herem,我正在寻找了解jQuery的一点点,所以我想我会尝试使用AJAX我的局部视图之间切换。
现在在我的用户的破折号我有一个链接到他们的喜欢页面,它需要全部重新加载看到'喜欢'网页,我将如何改变这个刷新的likes_user_path @用户)?
意见/页/ home.html.erb
< DIV ID =左>
< DIV ID =DASH-统计>
&所述; A HREF =&其中;%= likes_user_path(@user)%>中>
< DIV ID =喜欢级=STAT>喜欢
&其中;%= @ user.likes.count%GT;
< / DIV>
&所述; / a取代;
< / DIV>
< / DIV>
< DIV ID =权与GT;
< DIV ID =内容>
< / DIV>
< / DIV>
UsersController中
高清喜欢
@title =喜欢
@user = User.find(PARAMS [:ID])
@like_stuff = @ user.likes.paginate(:页=> PARAMS [:页面])
渲染show_likes
结束
我的理解是,你要重新加载喜欢
通过点击链接,通过使用AJAX,在用户的仪表板。
首先要做的就是改写链接,并添加:远程=>真正的
是这样的:
<%=的link_to喜欢,likes_user_path(@user):远程=>真%>
然后,在你的控制器,请确保您有一个 respond_to代码:HTML,:当然JS
上方(可以有其他的选择,但:JS
已在他们中间)
在这之后,你可以做一个调用的函数喜欢
在你的控制器,用于装载的喜欢,它看起来像你的,只有你不这样做渲染
结尾,但 respond_with @likes
。通过让:JS
在的respond_to
过滤器,Rails会自动地采取相应的行动时,应该
接下来,所谓相应的视图 likes.js.erb
(我不能完全肯定,如果CoffeeScript的将工作的开箱即用在这种情况下),在其中把类似
$('#喜欢)HTML('<%= escape_javascript(渲染喜欢,:喜欢=> @likes)%>')。
这假定在要渲染喜欢你的主要观点,有与的id元素=喜欢
这可能是这样的:
< DIV ID =喜欢>
<%=渲染喜欢,:喜欢=> @likes%>
< / DIV>
和也有一个 _likes.html.erb
部分这使得喜欢
<%likes.each办|喜欢| %>
< DIV>
&其中;%= like.name%GT;
< / DIV>
<%结束%GT;
和这个我想我pretty的很多遮盖。
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 likes
by clicking a link, by using ajax, in your user dashboard.
First thing to do is rewrite the link, and add a :remote => true
like this:
<%= link_to "Likes", likes_user_path(@user), :remote => true %>
Then, in your controller, make sure you have a respond_to :html, :js
at the top (there can of course be other options, but :js
has to be among them)
After that, you could do a function called likes
in your controller which to use for loading the likes, which looks something like yours, only you don't do render
at the end, but respond_with @likes
. By having :js
in the respond_to
filter, 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 like
$('#likes').html('<%= escape_javascript(render "likes", :likes => @likes) %>');
This assumes that in your main view in which you want to render the likes ,there's an element with the id=likes
which could look like this:
<div id="likes">
<%= render "likes", :likes => @likes %>
</div>
and there's also a _likes.html.erb
partial which renders the likes
<% likes.each do |like| %>
<div>
<%= like.name %>
</div>
<% end %>
And with this I think I pretty much covered it.
这篇关于轨道3:jQuery来改变谐音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!