Justin D.
Justin D.

Reputation: 4976

Ruby on Rails remote form not updating the page

UPDATE

I have mange to get it working by changing the Model call from

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

@comments = VideoComment.last(5).reverse

It works, but it gives me the last video comments from all the videos whereas I only want those from the current video (@video.id).

Any clue on how to do that?


I have a Videocontroller and a VideoComments controller which manages the comments for the Video controller. I am trying to make my remote form update the comments list with ajax but it does not seem to work. Can you find what I did wrong?

HTML code of the show page :

- 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 "

The Video_Commentscontroller createaction :

 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

The toggle.js.erb file which manages the page changes :

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

Upvotes: 1

Views: 429

Answers (1)

Ismael
Ismael

Reputation: 16720

If you are using Rails 3 you can do

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

Or if you have relations properly defined you can also do

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

Upvotes: 2

Related Questions