【问题标题】:Ruby on Rails remote form not updating the pageRuby on Rails 远程表单不更新页面
【发布时间】:2012-07-24 23:32:59
【问题描述】:

更新

我可以通过更改模型调用来使其正常工作

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

@comments = VideoComment.last(5).reverse

它有效,但它为我提供了所有视频中的最后一个视频,而我只想要当前视频中的那些 (@video.id)。

关于如何做到这一点的任何线索?


我有一个Videocontroller 和一个VideoComments 控制器,用于管理Video 控制器的cmets。我试图让我的远程表单使用 ajax 更新 cmets 列表,但它似乎不起作用。你能找出我做错了什么吗?

展示页面的HTML代码:

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

video_cmets/_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_Commentscontrollercreateaction:

 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 %>");

【问题讨论】:

  • 是@comments中的新评论吗?

标签: ruby-on-rails ruby ajax ruby-on-rails-3


【解决方案1】:

如果你使用的是 Rails 3,你可以这样做

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

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

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

【讨论】:

猜你喜欢
  • 2023-03-25
  • 2013-03-31
  • 2010-12-19
  • 2011-01-23
  • 1970-01-01
  • 2016-06-20
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多