0

My product_controller

class ProductController < ApplicationController
    
    def index
        @product = Product.all 
        @categories = Category.all
    end
    
    def from_category
        @product = Product.joins(:category).where(category: { id: params[:cat_id] })
        respond_to do |format|
            format.js
        end
    end
end

My form_category.js.erb

$("#products_grid").html("<%= escape_javascript(render partial: 'product/products_list', locals: { product: @selected } ) %>");

I want to filter products by categories and found this way, but I get an error on this part

enter image description here

dbugger
  • 15,868
  • 9
  • 31
  • 33
Jinny
  • 1
  • 2
  • 1
    if you called product/from_category directly from the browser, that's an HTML call. add a format.html block – dbugger Feb 02 '23 at 18:29
  • You need to specify the format is "js" if that is what you want to use [See Here](https://stackoverflow.com/questions/339130/how-do-i-render-a-partial-of-a-different-format-in-rails). You have not posted how you are actually trying to access this controller method so it is difficult to help here. – engineersmnky Feb 02 '23 at 20:40
  • I replaced it respond_to do |format| format.js end on this render formats: [ :html ] and now i have this error Security warning: an embedded – Jinny Feb 03 '23 at 07:43
  • and when i insert it protect_from_forgery except: :index or this skip_after_action :verify_same_origin_request from this source https://stackoverflow.com/questions/29310187/rails-invalidcrossoriginrequest my localhost shows this $("#products_grid").html(" – Jinny Feb 03 '23 at 07:44
  • I'm sorry, I'm trying to learn it all, but I don't understand everything, help me, please – Jinny Feb 03 '23 at 07:45
  • can you post the request from rails server logs? – O. Larkin Feb 03 '23 at 12:35
  • Processing by ProductController#from_category as HTML Parameters: {"cat_id"=>"1"} Rendering product/from_category.js.erb Product Load (0.2ms) SELECT "products".* FROM "products" ↳ app/views/product/_products_list.html.erb:1 Rendered product/_products_list.html.erb (Duration: 3.1ms | Allocations: 1805) Rendered product/from_category.js.erb (Duration: 3.7ms | Allocations: 2146) Completed 200 OK in 5ms (Views: 4.1ms | ActiveRecord: 0.2ms | Allocations: 2576) – Jinny Feb 04 '23 at 07:11

0 Answers0