SmartChart: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

Feature(s) impacted

  • Smart Charts through API : I created a “leaderboard” chart using the API option to display some data.

Here is my controller:

class Forest::ChartsController < ForestLiana::ApplicationController
	
	require 'google/apis/webmasters_v3'
	require 'googleauth'
	require 'net/http'
	require 'uri'
	require 'json'

  def non_optimized_articles
		# logic to get data

		stat = ForestLiana::Model::Stat.new({ value: data })
		render json: serialize_model(stat)
  end
end

config/application.rb:

module Lalanguefrancaisev3
  class Application < Rails::Application

    null_regex = Regexp.new(/\Anull\z/)
    config.middleware.insert_before 0, Rack::Cors do
      allow do
        hostnames = [null_regex, 'localhost:4200', 'app.forestadmin.com', 'localhost:3001']
        hostnames += ENV['CORS_ORIGINS'].split(',') if ENV['CORS_ORIGINS']
        origins hostnames
        resource '*',
          headers: :any,
          methods: :any,
          expose: ['Content-Disposition'],
          credentials: true
      end
    end
  end
end

Observed behavior

I get an error (see below for full logs):

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)

The weird thing is WpPostsController#create is called, I don’t know why…

Expected behavior

  • The chart should display the data properly.

Failure Logs

Started POST “/stats/non-optimized-articles?timezone=Europe%2FIstanbul” for ::1 at 2022-09-17 09:47:18 +0300
Processing by WpPostsController#create as JSON
Parameters: {“type”=>“Leaderboard”, “limit”=>nil, “timezone”=>“Europe/Istanbul”, “category”=>“stats”, “subcategory”=>“non-optimized-articles”, “wp_post”=>{}}
HTTP Origin header (https://app.forestadmin.com) didn’t match request.base_url (http://localhost:3000)
Completed 422 Unprocessable Entity in 2ms (Allocations: 761)

Started POST “/stats/non-optimized-articles?timezone=Europe%2FIstanbul” for ::1 at 2022-09-17 09:47:18 +0300

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)

Context

  • Project name: La langue française
  • Environment name: Development / Production
  • Ruby version: 3.1.2
  • Rails version: 6.1.6
  • Forest liana version: ‘~> 7.0’, ‘< 7.1.0’

Thanks.

Hello @lumberjack,
Any update on this?

I am really not sure why WpPostsController#create is getting called and why there is “wp_post”=>{} in parameters.

Thanks for your help!

Hey @nico_lrx , and welcome to our community :wave:

When using a smart chart via the api, you must declare a custom endpoint in your app.

Could you check that you have the following route in your config/routes.rb :

namespace :forest do
    post '/stats/non-optimized-articles' => 'charts#non_optimized_articles'
end

Note that this route must be declared before other forest routes.

mount ForestLiana::Engine => '/forest'

There is a full example in our documentation which may help you : Create an API-based Chart - Developer guide

If the problem persists, could you share your browser’s network log? :pray:

Hello @matthv,
Thanks for your reply. Here are my Forest routes:

namespace :forest do
    post '/actions/generate-newsletter-campaign' => 'newsletters#generate_newsletter_campaign'
    post '/actions/create-new-word' => 'words#create_new_word'
    post '/actions/search-variant' => 'words#search_variant'
    post '/actions/generate-audio-url' => 'quiz_questions#generate_audio_url'
    post '/actions/generate-audio-url' => 'quiz_questions#generate_audio_url'
		post '/stats/non-optimized-articles' => 'charts#non_optimized_articles'
  end
  mount ForestLiana::Engine => '/forest'

If I change “charts#non_optimized” to “ChartsController#non_optimized” I get the following error:

‘forest/ChartsController’ is not a supported controller name.

Note that “charts_controller.rb” is located in app/controllers/forest/charts_controller.rb

Here are the network logs in development environment. Please let me know if you need some details:

Thanks!

I’m sorry there was a typo in my previous message.

Your routing is good :+1:

> post '/stats/non-optimized-articles' => 'charts#non_optimized_articles'

For more context, the application does not recognize your route and treats the route format as a collection route.

I will try to reproduce your issue on my end.
Do you have more than one ChartController in your application?

I don’t have any other ChartController. Thanks!

I can’t reproduce on my end, however in the settings section, the prefix ‘forest’ is missing into the field API endpoint url.

Could you try with this value: /forest/stats/non-optimized-articles :pray:

3 Likes

It works, thanks a lot!

1 Like