All threads / How do I render a partial which contains a json object and iterate over the json object and appends it to where I want it too!! Frustration level 1000

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I render a partial which contains a json object and iterate over the json object and appends it to where I want it too!! Frustration level 1000

PDG Creative asked in General

So here is the Scenario of what I am trying to do

I have a view

              = link_to admin_page_path(page), class: "edit-#{page.id}" do
                .subcategory.btn.btn-xs.btn-info{"data-subcategory" => page.id}
                  View Subcategories

Here is my javascript

:javascript
  $('.subcategory').on('click', function(e){
    e.preventDefault();

    row = $(this)
    page_id = $(this).data('subcategory');
    console.log(page_id);

    Rails.ajax({
      url: "/admin/pages/" + page_id,
      type: "get",
      dataType: "json",
      success: function(data) {
          /*render partial with data*/ 
     },
      error: function() {
        console.log('the covids')
      }
    })
  })

Here is the controller

  def show
    @page = Page.find(params[:id])
    @page_subcategories = @page.subcategories
    render json: @page_subcategories
     how can I render a partial with the @page_subcategories json response?
  end

How can I get @page_subcategories into a partial which than appends to the specific table row id

Please !

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 53,406+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.