ruby 在 rails 调用外部 api 并显示结果
ruby on rails call external api and show results
我正在尝试调用 API(点赞 https://jsonplaceholder.typicode.com/)。这个想法很简单,点击按钮显示资源,但我在实现它时遇到了麻烦。我下面的只是调用该函数并想在控制台中打印它的按钮。
错误:
enter code here No route matches [POST] "/dashboard/fetch_action"
路线
resources :dashboard do
collection do
get :fetch_action
end
end
dashboard_controller.er
require 'faraday'
def index
end
def fetch_action
response = Faraday.get 'https://jsonplaceholder.typicode.com/posts/1'
puts response
# return response
end
index.html.erb
<%= button_to "fetch action", fetch_action_dashboard_index_path, class: "btn btn-warning" %>
谢谢
编辑:
路线:
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
root to: 'devise/sessions#new'
end
get 'dashboard' => 'dashboard#index'
get '/dashboard/fetch-action', to: 'dashboard#fetch_action', as: 'dashboard_fetch_action'
get '/user' => 'dashboard#index', :as => :user_root
end
耙路
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
index.html.eb
<%= link_to "fetch action", dashboard_fetch_action_path, class: "btn btn-info" %>
<%= @response %>
获取-action.html.erb
<%= @response %>
dashboard_controller
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
错误(单击 link_to 重定向到 dashboard/fetch-action 时出现以下错误):
DashboardController#show is missing a template for this request format and variant. request.formats: ["text/html"] 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.
故障排除和设置路由
首先,在您的终端中,运行 rake routes
打印您应用中的路线。 Post 输出到这里。但是您需要检查按钮中使用的 fetch_action_dashboard_index_path
命令的输出 - 这是正确的路径吗?
我想不是。还有一种更简单的方法来声明一条路线。您在路由中使用资源声明所做的是为仪表板控制器的索引、显示、创建、更新和删除操作配置路由。 routes.rb 文件中的以下行将仅设置 fetch_action 路由并将其命名为“dashboard_fetch_action”
get '/dashboard/fetch-action', to: 'dashboard#fetch_action', as: 'dashboard_fetch_action'
正在创建通往该路线的 link
第二;使用 link_to
而不是 button_to 来确保您发出 GET 请求。您正在应用的 btn 类 将使 link 看起来像一个按钮,并且 a
标签在语义上比 button
更正确(因为您想要 GET)所以使用:
<%= link_to "fetch action", dashboard_fetch_action_path, class: "btn btn-warning" %>
呈现动作的结果
最后确保您在 app/views/dashboard/fetch_action.html.erb
中有一个文件,它将 API 响应呈现为 HTML。此内容(或类似内容)将实现这一目标。
<%= @response =>
并且,在仪表板控制器中的 fetch_action 操作中。使用 @response = Faraday...
而不是 response = Faraday...
。这样就可以在视图 HTML 中访问 response
对象(这就是 @ 所表示的)
def fetch_action
@response = Faraday.get 'https://jsonplaceholder.typicode.com/posts/1'
end
您是否尝试过将模板重命名为 fetch_action.html.erb
?
据我所知,Rails希望模板名称与操作名称匹配,这可能是 fetch_action
因为 Ruby 方法不能包含破折号 -
.破折号用于减法,这就是它在方法名称中无效的原因。
我正在尝试调用 API(点赞 https://jsonplaceholder.typicode.com/)。这个想法很简单,点击按钮显示资源,但我在实现它时遇到了麻烦。我下面的只是调用该函数并想在控制台中打印它的按钮。
错误:
enter code here No route matches [POST] "/dashboard/fetch_action"
路线
resources :dashboard do
collection do
get :fetch_action
end
end
dashboard_controller.er
require 'faraday'
def index
end
def fetch_action
response = Faraday.get 'https://jsonplaceholder.typicode.com/posts/1'
puts response
# return response
end
index.html.erb
<%= button_to "fetch action", fetch_action_dashboard_index_path, class: "btn btn-warning" %>
谢谢
编辑:
路线:
Rails.application.routes.draw do
devise_for :users
devise_scope :user do
root to: 'devise/sessions#new'
end
get 'dashboard' => 'dashboard#index'
get '/dashboard/fetch-action', to: 'dashboard#fetch_action', as: 'dashboard_fetch_action'
get '/user' => 'dashboard#index', :as => :user_root
end
耙路
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
index.html.eb
<%= link_to "fetch action", dashboard_fetch_action_path, class: "btn btn-info" %>
<%= @response %>
获取-action.html.erb
<%= @response %>
dashboard_controller
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
错误(单击 link_to 重定向到 dashboard/fetch-action 时出现以下错误):
DashboardController#show is missing a template for this request format and variant. request.formats: ["text/html"] 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.
故障排除和设置路由
首先,在您的终端中,运行 rake routes
打印您应用中的路线。 Post 输出到这里。但是您需要检查按钮中使用的 fetch_action_dashboard_index_path
命令的输出 - 这是正确的路径吗?
我想不是。还有一种更简单的方法来声明一条路线。您在路由中使用资源声明所做的是为仪表板控制器的索引、显示、创建、更新和删除操作配置路由。 routes.rb 文件中的以下行将仅设置 fetch_action 路由并将其命名为“dashboard_fetch_action”
get '/dashboard/fetch-action', to: 'dashboard#fetch_action', as: 'dashboard_fetch_action'
正在创建通往该路线的 link
第二;使用 link_to
而不是 button_to 来确保您发出 GET 请求。您正在应用的 btn 类 将使 link 看起来像一个按钮,并且 a
标签在语义上比 button
更正确(因为您想要 GET)所以使用:
<%= link_to "fetch action", dashboard_fetch_action_path, class: "btn btn-warning" %>
呈现动作的结果
最后确保您在 app/views/dashboard/fetch_action.html.erb
中有一个文件,它将 API 响应呈现为 HTML。此内容(或类似内容)将实现这一目标。
<%= @response =>
并且,在仪表板控制器中的 fetch_action 操作中。使用 @response = Faraday...
而不是 response = Faraday...
。这样就可以在视图 HTML 中访问 response
对象(这就是 @ 所表示的)
def fetch_action
@response = Faraday.get 'https://jsonplaceholder.typicode.com/posts/1'
end
您是否尝试过将模板重命名为 fetch_action.html.erb
?
据我所知,Rails希望模板名称与操作名称匹配,这可能是 fetch_action
因为 Ruby 方法不能包含破折号 -
.破折号用于减法,这就是它在方法名称中无效的原因。