Karthick M
Karthick M

Reputation: 779

How to send the count value using JSON in ROR

I want to send the count value,for count the post i am using thumbs up gem in ROR.

Now i want to send the count in json,the vote as post action in def show

def index
@posts = Post.all
respond_with(@posts) do |format|
  format.json { render json: @post_names = {:post => @posts.as_json(:only=> [:content, :title]) } }

end
end 

I want to send the count value using json,because i want to show that count value in my client side.

Upvotes: 0

Views: 814

Answers (2)

Free-Minded
Free-Minded

Reputation: 5430

You can send vote count by this way. I assume something like this can work..

def index
  @posts = Post.all
  respond_with(@posts) do |format|
    format.json { render :json => @post_names = {:post => @posts.map {|t| {:title => t.title, :content => t.content, :count => t.votes_for }} } }
  end
end

Upvotes: 3

ezkl
ezkl

Reputation: 3861

Since you're working on the controller, you might find it helpful to look at the render section of the Rails documentation.

Upvotes: 1

Related Questions