title: ruby on rails call external api and show results
body: <p>I'm trying to call API (to like <a href="https://jsonplaceholder.typicode.com/" rel="nofollow noreferrer">https://jsonplaceholder.typicode.com/</a>). the idea is simple click the button to show the res but I'm having trouble implementing it. What I have below is just the button calling the function and want to print it in the console.</p>
  <p>error:</p>
  <p><code>enter code here No route matches [POST] &quot;/dashboard/fetch_action&quot;</code></p>
  <p>routes</p>
  <pre><code>resources :dashboard do
   collection do
     get :fetch_action
   end
  end
  </code></pre>
  <p>dashboard_controller.er</p>
  <pre><code>require 'faraday'
  
  def index
  end
  
  def fetch_action
      response = Faraday.get 'https://jsonplaceholder.typicode.com/posts/1'
      puts response
      # return response 
  end
  </code></pre>
  <p>index.html.erb</p>
  <pre><code>    &lt;%= button_to &quot;fetch action&quot;, fetch_action_dashboard_index_path, class: &quot;btn btn-warning&quot; %&gt;
  </code></pre>
  <p>Thank you</p>
  <p>EDIT:</p>
  <p>routes:</p>
  <pre><code>Rails.application.routes.draw do
  
    devise_for :users
    
    devise_scope :user do
      root to: 'devise/sessions#new'
    end
  
    get 'dashboard' =&gt; 'dashboard#index'
    get '/dashboard/fetch-action', to: 'dashboard#fetch_action', as: 'dashboard_fetch_action'
    get '/user' =&gt; 'dashboard#index', :as =&gt; :user_root
  
  end
  </code></pre>
  <p>rake routes</p>
  <pre><code>    new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
               user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
       destroy_user_session DELETE /users/sign_out(.:format)                                                                devise/sessions#destroy
          new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
              user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                            PUT    /users/password(.:format)                                                                devise/passwords#update
                            POST   /users/password(.:format)                                                                devise/passwords#create
   cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
      new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
     edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
          user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                            PUT    /users(.:format)                                                                         devise/registrations#update
                            DELETE /users(.:format)                                                                         devise/registrations#destroy
                            POST   /users(.:format)                                                                         devise/registrations#create
                       root GET    /                                                                                        devise/sessions#new
                  dashboard GET    /dashboard(.:format)                                                                     dashboard#index
     dashboard_fetch_action GET    /dashboard/fetch-action(.:format)                                                        dashboard#fetch_action
                  user_root GET    /user(.:format)                                                                          dashboard#index
         rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
  rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
         rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
  update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
       rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
             
                                                                                       
  </code></pre>
  <p>index.html.eb</p>
  <pre><code>&lt;%= link_to &quot;fetch action&quot;, dashboard_fetch_action_path, class: &quot;btn btn-info&quot; %&gt;
  
  &lt;%= @response %&gt;
  </code></pre>
  <p>fetch-action.html.erb</p>
  <pre><code>&lt;%= @response %&gt;
  </code></pre>
  <p>dashboard_controller</p>
  <pre><code>def index
  end
  
  def show
  end
  
  def fetch_action
      uri = URI('https://jsonplaceholder.typicode.com/posts/1')
      req = Net::HTTP.get(uri)
      @response = JSON.parse(req)
  end
  </code></pre>
  <p>Error (when clicking the link_to redirects to dashboard/fetch-action with the following error):</p>
  <p><code>DashboardController#show is missing a template for this request format and variant. request.formats: [&quot;text/html&quot;] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.</code></p>