lundi 26 octobre 2015

Why this won't load partial when using ajax loading?

Why this won't load partial?(chat comment DOM) But it loads the url /comments instead.

How can I load only partial? What am I missing here?

routes.rb

.
.
.
resources :comments
get ":lang/v/:site/:uid/refresh_part_after_comment" => 'movies#refresh_part_after_comment'
get ':lang/v/:site/:uid' => 'movies#show', :via => :get, :as => :watch_v
resources :movies
.
.
.

views/movies/show.html.erb (input form here!)

.
.
.
<div id="chat_comment">
    <%= render 'movies/comment' %>
</div>

<form action="/comments" method="post" data-remote="true" >
    <input type="text" name="body" size="50" id="body_input"/>
    <input type="hidden" name="elapsed_time" id="elapsed_time">
    <input type="hidden" name="video_id" value="<%= params[:uid] %>">
    <button type="submit" >Submit</button>
</form>
.
.
.

comments_controllr.rb

class CommentsController < ApplicationController
    def create

        if @user = User.find_by_twitter_id(session[:id])
        else
            @user = User.new
            @user.twitter_id = session[:id]
            @user.save
        end

        if @movie = Movie.find_by_uid(params[:video_id])

        else
            @movie = Movie.new
            @movie.uid = params[:video_id]
            @movie.save
        end

        @comment = Comment.build_from(@movie, @user.id, params[:body]) 
        @comment.elapsed_time = params[:elapsed_time]
        @comment.save

        flash[:notice] = "posted"       

        respond_to do |format|      
            format.js do
                render 'en/v/yt/' + @movie.uid + '/refresh_part_after_comment'
            end
        end

    end
end

movies_controller.rb

class MoviesController < ApplicationController
    .
    .
    .
    def refresh_part_after_comment
        if @movie = Movie.find_by_uid(params[:uid]) 
            @comments = @movie.comment_threads.order("elapsed_time ASC")
        end

        respond_to do |format|
            format.js 
        end
    end

    def show
        if @movie = Movie.find_by_uid(params[:uid]) 
            @comments = @movie.comment_threads.order("elapsed_time ASC")
        end

        respond_to do |format|
            format.html # show.html.erb
            format.json { render json: @movie }
        end
    end
    .
    .
    .
end

views/movies/refresh_part_after_comment.js.erb

$('#chat_comment').html("<%= j(render(:partial => 'movies/comment')) %>");

views/movies/_comment.html.erb

<% @comments.each do |comment| %>
    <%= comment.elapsed_time %>_<%= comment.body %><br />
<% end %>

Aucun commentaire:

Enregistrer un commentaire