WQ.Kevin WQ.Kevin - 3 years ago 198
JSON Question

Rails multiple format.json

How can I write multiple format.json responses like:

def index

@books = Book.all
@user = @books.map { |u| u.user }.uniq

respond_to do |format|
format.html
format.json { render json: @books }
format.json { render json: @user }
end

end


in a better way?

Answer Source

You cannot use multiple block for same format. If you want to render both books and users try sending them at once:

def index 

  @books = Book.all
  @user = @books.map { |u| u.user }.uniq

  respond_to do |format|
    format.html
    format.json { render json: {books: @books, users: @user} }
  end

end
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download