Kellogs
Kellogs

Reputation: 471

Issue with Rendering Ajax/Rails

Situation: Currently have a comment model that paginates under a micropost. I am trying to make the next button render comments that belong to the micropost.

Issue: I am unsure how to go about making a route/action inorder to bring these comments through.

Codes: I have some code that I will provide below, if anything isn't right please assist.

All help is much Appreciated.

References: Issue with Ajax Appending

User Controller

  def show
    @user = User.find(params[:id])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @micropost.comments.build(params[:comment])
    @microposts = @user.microposts.order('created_at DESC').paginate(:per_page => 10, :page => params[:page])
  end

Pagination JS

$("#CommentPagin").on('click', 'a', function(e){
    // Get data from server - make sure url has params for per_page and page.
    $.get($(this).attr('href'), function(data){
        // refresh client with data
        $("#cc").append(data);
    });
});

Comment Section

<div id='comments'>
  <% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
  <div id="CommentPagin">
  <span class="CommentArrowIcon"></span>
  <%= will_paginate comments, :page_links => false , :class =>"pagination" %>
  </div>
<%= render 'users/comments' %>
</div>

Comment Rendering Section

<div id="cc">
<% comments = micropost.comments.paginate(:per_page => 5, :page => params[:page]) %>
<%= render comments %>
</div>

UPDATE

User Model

class User < ActiveRecord::Base
  has_many :microposts
  has_many :comments
end

Micropost Model

class Micropost < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  accepts_nested_attributes_for :comments
end

Comment Model

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id, :micropost_id
  belongs_to :micropost, :counter_cache => true
  belongs_to :user
  belongs_to :school
end

Routes.rb

kit::Application.routes.draw do
  resources :pages
  resources :application
  resources :schools
  resources :microposts
  resources :comments
  resources :users
  resources :sessions
  resources :password_resets
  resources :relationships, only: [:create, :destroy]
  resources :users do
      member do
        get :following, :followers
      end
  end
  resources :microposts do
    member do
      post :vote_up, :unvote
    end
  end
  resources :microposts do
    member do
      post :upview
    end
  end
  resources :microposts do
    resources :comments
  end
  resources :schools do
    collection do
        get :mostrecent
        get :mostdiscussed
        get :highestrated
        get :viewcount
    end
  end

  match "/users/:id/personalstream" => "users#personalstream"
  match "/schools/:id/mostrecent" => "schools#mostrecent"
  match "/schools/:id/mostdiscussed" => "schools#mostdiscussed"
  match "/schools/:id/viewcount" => "schools#viewcount"
  match "/schools/:id/highestrated" => "schools#highestrated"  
  match "/schools/:id", :to => 'schools#show', :as => "school"
  match "/microposts/:id/comments" => "microposts#comments"
  match "/microposts/:id/upview" => "microposts#upview"
  match "/microposts/:id/vote_up" => "microposts#vote_up"
  match "/microposts/:id/unvote" => "microposts#unvote"
  get "log_out" => "sessions#destroy", :as => "log_out"
  get "sign_in" => "sessions#new", :as => "sign_in"
  get "sign_up" => "users#new", :as => "sign_up"
  get "home" => "users#home", :as => "home"

  root to: "schools#index"

end

Upvotes: 1

Views: 193

Answers (1)

klump
klump

Reputation: 3269

add a new action to the microposts controller:

app/controllers/microposts_controller.rb
def comments
  @micropost = Micropost.find(params[:id])
  @comments = @micropost.comments

  # we dont need all the html head stuff
  render :layout => false
end

write a view (app/views/microposts/comments.html.erb) where you display all the @comments as you want

and add a new member to you microposts resource get :comments

now you can try in the browser /microposts/(add a micropost id here)/comments this should deliver you all the comments for the user and format them as you wish.

the last part is the simplest: when the user clicks on the div, make a request to this site via ajax and attach the answer to the div where you want to display the comments. the code could look like this:

$("#CommentPagin").click( function(){
  $("#CommentPagin").load( "<%= micropost_comments_path( @user ) %>" );
});

hope you got an idea. report back if its not working

Upvotes: 1

Related Questions