Setting up a `GET` Smart Action

I’ve been following the docs to set up a simple smart action (“show in app”) in my Forest Admin panel for my Rails app, but it’s not working!

I’ve tried a couple of ways now.

Ideally, it’s a GET request since I’m not changing any data, just opening a page within my app.

I have the following code.

In lib/forest_liana/collections/book.rb:

  action 'Show in app', type: 'single', endpoint: '/forest/actions/books/show-in-app', http_method: 'GET'

In config/routes.rb:

  namespace :forest do
    get '/actions/books/show-in-app' => 'books#show_in_app'
  end

  mount ForestLiana::Engine => '/forest'

In app/controllers/forest/books_controller.rb:

class Forest::BooksController < ForestLiana::SmartActionsController
  def show_in_app
    book_id = ForestLiana::ResourcesGetter.get_ids_from_request(params).first
     
    render json: {
      success: 'Success',
      redirectTo: book_path(book_id),
    }
  end
end

When I attempt to use the action, I get the following failure in the console:

TypeError: Failed to execute ‘fetch’ on ‘Window’: Request with GET/HEAD method cannot have body.

I tried to find a way for the request to not send any data along with it and failed. If there’s a way to do that, I’d love to know.

In the meantime, I decided to just use the default POST method to see if I could get the action working.

Once I got to the proper CORS setup, to enable app.forestadmin.com to make an API request to my localhost, I get a 403 with the following error, whether I try and redirect to localhost or an HTTPS ngrok tunnel link:

Filter chain halted as :check_permission_for_smart_route rendered or redirected

I’m not sure if this is related to user/role permissions on the smart action but the docs say this shouldn’t be a factor for development environment.

I’m on Rails 6.1, using forest_liana 6.4.

Thank you.

Hi @Nadia,

By

just opening a page within my app

What is the expected result?
Do you want a new browser tab to open to the address books#show_in_app ?

As I understand, when the books collections is opened, you want a smart action to navigate to a book record. I should miss something because you just have to click on a book to navigate to it.

Regards

Hi @Sliman_Medini,

I want a new browser tab to open to the specific record on my website/app, not within Forest! So, for a book record in Forest, I’ll click “Show in app” and a new tab will open on my customer-facing website, with the book’s page, title, cover, etc.

Thank you,

Nadia

Understood.

This section of the doc helps to do this.

To be the simplest, please use POST smart action route in your server.

So, the need for your smart action is to return:

{
  success: 'success message',
  redirectTo: 'https://url-to-app'
}

like that Forest will open a tab onto https://url-to-app

Let’s focus on the request made by the browser when you trigger the smart action.
Is the status 200, is it POST, what is the response body from your server?

Hi @Sliman_Medini,

I’ve been using those docs, shared all of my code (both GET and POST variations) and explained exactly what I was seeing, including the status code, in my original post.

Did you see all of that?

Thanks,

Nadia

Yes @Nadia, I see all of that :slight_smile:

The development environments should allow you to have triggerable smart actions without any roles enabled. I’ve just validated this locally their is no apparent bug on this :ok_hand:

Since the issue ‘check_permission_for_smart_route’ indicates an error with the permissions validation, I am asking how it was possible.

While I am still investigating this, can you please test this: grant all permissions on your user role and retry, just to see if it changes something. If nothing changed with all permissions, it’s an indication we must have another problem.

Regards

So I have an issue where it says I can’t edit the settings because I can’t enter layout mode in my Dev environment without creating a branch.

I thought I needed to create a new branch in my code, but that didn’t seem to work.

How do I create a new branch?

Thank you, @Sliman_Medini.

EDIT: Never mind, just seen how:

Also, I’ve just had to deploy to production to get some other code out and I enabled the roles and trigger permissions there and it still fails, so I figure that permissions isn’t the issue!

This is the error in my console:

Ok, thank you @Nadia,
Are you able to run other smart actions in the same environment? And for other environments?

Regards

Hi again @Nadia,

Thank you for your patience.
The described issue was reproduced.
A workaround is to remove the first slash in the endpoint declaration:

action 'Show in app', type: 'single', endpoint: 'forest/actions/books/show-in-app', http_method: 'POST'

Thank you for your feedback, we will fix the slash problem.
Regards

Hi @Sliman_Medini,

Yay, it works now!

Thank you for your time and for letting me know.

Nadia

1 Like