mercredi 22 juillet 2015

Why this won't refresh partial after ajax loading?

It creates new comment record but it won't reload partial:(
It just shows a white blank page right after I clicked on submit button.
Why? and How can I fix this?

views/movies/show.html.erb

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

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

/config/routes.rb

resources :comments
get "movies/:uid/refresh" => 'movies#refresh'

controllers/comments_controller.rb

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.save

    flash[:notice] = "Posted!"

    respond_to do |format|  
        format.js do
            render 'movies/refresh'
        end 
    end     
end

controllers/movies_controller.rb

def refresh
    @movie = Movie.find_by_uid(params[:uid])
    @comments = @movie.comment_threads.order("created_at DESC")

    respond_to do |format|
        format.js 
    end
end

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

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

views/movies/refresh.js.erb

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

views/movies/_comment.html.erb

<% if @comments %>
    <% @comments.each do |comment| %>
        <%= comment.id %>:<%= comment.body %><br />
    <% end %>
<% end %>

Aucun commentaire:

Enregistrer un commentaire