2012-07-24 73 views
1

UPDATERuby on Rails的远程形成不更新页面

我已经奶源得到它通过改变从

@comments = VideoComment.all(:conditions => { :video_id => @video.id}, :limit => 5, :order => :created_at) 

@comments = VideoComment.last(5).reverse 

它的工作原理模型调用的工作,但它给我的最后一个视频评论从全部的视频,而我只想从当前的视频(@video.id)。

任何线索如何做到这一点?


我有一个Video控制器和VideoComments控制器,它管理的Video控制器的意见。我试图让我的远程表单用ajax更新评论列表,但它似乎不起作用。你能找到我做错了什么吗?显示页面的

HTML代码:

- if current_user 
    #comment-form 
     = render 'video_comments/comment_form' 
    %ul 
    #comments 
     = render @comments 

video_comments/_comment_form.html.haml

= form_for current_user.video_comments.build(:video_id => params[:id]), :remote => true do |f| 
    .form-fields 
    .comment-content 
     = f.text_area :content, rows:2 
    = f.hidden_field :video_id 
    = f.hidden_field :user_id 
    .submit-form 
     = f.submit "Add a comment", :class => "btn btn-default " 

Video_Comments控制器create行动:

def create 
    @comment = VideoComment.create(params[:video_comment]) 
    @video = @comment.video 
    @comments = VideoComment.all(:conditions => { :video_id => @video.id}, :limit => 5, :order => :created_at) 
    render :toggle  
end 

toggle.js.erb文件,该文件管理页面更改:

$("#comment-form").html("<%= escape_javascript render 'comment_form' %>"); 
$("#comments").html("<%= escape_javascript render @comments %>"); 
+0

是'@ comments'新的评论? – 2012-07-25 00:05:52

回答

2

如果您使用Rails 3,你可以做

@comments = VideoComment.where(:video_id => @video.id).order(:created_at).limit(5) 

或者,如果你有关系的正确定义,你也可以做

@comments = @video.comments.order(:created_at).limit(5) 
+0

你再次ismaelga! :P第二种解决方案就是我一直在寻找的东西! – 2012-07-25 00:17:15

+0

你可能想看到这篇文章http://m.onkey.org/active-record-query-interface – 2012-07-25 00:27:00