我在rails-api应用程序上使用CarrierWave进行图像上传,而后者又由客户端应用程序使用.我注意到,当没有可用的默认图像时,它会返回.它反过来必须.以下是我的配置设置:backbone.js/assets/default.pnghttp://dev-server:3000/assets/default.png
# config/environments/development.rb
CarrierWave.configure do |config|
config.asset_host = "http://dev-server:3000"
end
# Image Uploader
....
def default_url
"/assets/default.png"
end
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我已经提到了这个链接
创建我自己的api但遇到路由错误,因为这是第一次使用命名空间.
这是我的控制器
class API::IndexController < ApplicationController
def index
@clients = Client.all
respond_to do |format|
format.json
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的路线有
namespace :api do
resources :index
end
Run Code Online (Sandbox Code Playgroud)
这是我的inflection.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
Run Code Online (Sandbox Code Playgroud)
一旦我完成它我尝试localhost:3000/api/index.json,但抛出错误为
Routing Error
uninitialized constant API
Run Code Online (Sandbox Code Playgroud)
谁能帮忙.
我只创建一个Rails 5 API,我需要创建一个实时的Web通知.
这可以通过使用ActionCable吗?有没有人有行动电缆或任何其他解决方案的例子?
提前致谢.
我在API模式下使用Rails 5启动了一个新项目。
目前,我只是创建项目,设置数据库并使用scaffold命令。
rails g scaffold User
Run Code Online (Sandbox Code Playgroud)
我尝试用邮递员的代码创建一个新用户(POST)
请求
URI localhost:3000/v1/users
{
"first_name": "Firstname",
"last_name": "Lastname",
"email": "anemail@gmail.com32"
}
Run Code Online (Sandbox Code Playgroud)
结果
#<NoMethodError: undefined method `user_url' for #<Api::V1::UsersController:0x005594d8a4ad90>
Run Code Online (Sandbox Code Playgroud)
我该如何解决该错误?
routes.rb
Rails.application.routes.draw do
scope module: 'api' do
namespace :v1 do
resources :users, :as => 'user'
end
end
end
Run Code Online (Sandbox Code Playgroud)
users_controller.rb
# POST /users
def create
@user = User.new(user_params)
if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
Run Code Online (Sandbox Code Playgroud) 想要为附加文件添加 url,同时响应对父资源(比如 Person)的嵌套资源(比如文档)的请求。
# people_controller.rb
def show
render json: @person, include: [{document: {include: :files}}]
end
# returns
# {"id":1,"full_name":"James Bond","document":{"id":12,"files":[{"id":12,"name":"files","record_type":"Document","record_id":689,"blob_id":18,}]}
# MODELS
# person.rb
class Person < ApplicationRecord
has_one :document, class_name: "Document", foreign_key: :document_id
end
# document.rb
class Document < ApplicationRecord
has_many_attached :files
end
Run Code Online (Sandbox Code Playgroud)
问题是,我想在 React 前端设置中显示该文件或提供指向该文件的链接,该设置没有像 url_for 这样的辅助方法。正如这里所指出的。
任何帮助将不胜感激。
我正在构建 2 个应用程序;一个前端,一个后端。
后端将使用 Rails API + Doorkeeper Gem(oauth2 提供程序)构建,而前端将使用 React Native 构建。
目前,我正在使用“客户端凭据授予流程”,它目前工作正常。但是经过一段时间的研究,这个流程不应该用于客户端应用程序,因为它会暴露client_secret是否有人反编译应用程序。
我还阅读了“隐式授权流”,它只需要client_id. 但是这个流程现在看起来有点老了?
根据这个:https : //auth0.com/docs/api-auth/which-oauth-flow-to-use#is-the-client-a-single-page-app-
建议使用“带有 PKCE 的授权代码授予”而不是“隐式授予流程”。我能够让它工作,但问题是它仍然需要client_secret为了获得access_token,这是应该的吗?
这是我正在做的示例流程:
curl -X POST 'http://localhost:3000/oauth/authorize?client_id=xxxx&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=public&code_challenge=testchallenge&code_challenge_method=plain'
Run Code Online (Sandbox Code Playgroud)
这会给我以下回复:
{
"status": "redirect",
"redirect_uri": {
"action": "show",
"code": "8quZ-EAiKKG2EKnQiSYs3xeFRCgsIwcTbaWNdjnpyFg"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我将使用上面的代码使用以下请求获取访问令牌:
curl -i http://localhost:3000/oauth/token \
-F grant_type="authorization_code" \
-F client_id="xxxx" \
-F client_secret="xxxx" \
-F code="8quZ-EAiKKG2EKnQiSYs3xeFRCgsIwcTbaWNdjnpyFg" \
-F redirect_uri="urn:ietf:wg:oauth:2.0:oob" \
-F code_verifier="testchallenge"
Run Code Online (Sandbox Code Playgroud)
现在问题来了,为了交换code到一个access_token我仍然需要client_secret. 如果两者都只公开我的client_secret. …
我目前正在构建一个API并尝试测试我的控制器.我收到了与Devise相关的错误,我无法解决.这是我目前拥有的:
# /spec/requests/api/v1/sessions_spec.rb
require 'spec_helper'
describe API::V1::SessionsController do
describe "POST /sessions" do
it "on success, returns the user's authentication token" do
user = FactoryGirl.create(:user)
user_params = {
"user" => {
"email" => user.email,
"password" => user.password
}
}.to_json
post "/api/v1/sessions", user_params
expect(response.status).to eq(200)
json = JSON.parse(response.body)
expect(json['authentication_token']).to eq(user.authentication_token)
end
end
end
# /app/controllers/api/v1/sessions_controller.rb
class API::V1::SessionsController < Devise::SessionsController
respond_to :json
def create
end
end
# routes.rb
MyApplication::Application.routes.draw do
namespace :api, defaults: { format: :json } do
namespace :v1 do
devise_scope :user …Run Code Online (Sandbox Code Playgroud) 我发送的Json响应是这样的
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null
},
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
Run Code Online (Sandbox Code Playgroud)
我希望video_url也与广告对象合并.
我现在发送回复的方式是
render json: {:success=>true, :message=>"Ad detail",:ad=>@ad, :video_url => @ad.video.url}, :status=>200
Run Code Online (Sandbox Code Playgroud)
我如何将其与广告对象合并?
我想发送它
"ad": {
"id": 3,
"title": "dgdfg",
"description": "kjlj",
"video_file_name": "SampleVideo_1080x720_1mb.mp4",
"thumbnail_file_name": "images.jpeg",
"campaign_id": null,
"duration": null,
"video_url": "/system/ads/videos/000/000/003/original/SampleVideo_1080x720_1mb.mp4?1448019186"
}
Run Code Online (Sandbox Code Playgroud)
我的@ad目标是
#<Ad:0x007efc20495f98
id: 3,
title: "dgdfg",
description: "kjlj",
video_file_name: "SampleVideo_1080x720_1mb.mp4",
video_content_type: "video/mp4",
video_file_size: 1055736,
video_updated_at: Fri, 20 Nov 2015 11:33:06 UTC +00:00,
thumbnail_file_name: "images.jpeg",
thumbnail_content_type: "image/jpeg", …Run Code Online (Sandbox Code Playgroud) 使用rails --api预期的 rails 作为PUT方法从POST and params[:_method]='put'路由,但路由为POST
考虑以下:
配置/routes.rb:
resources :sessions, do
put 'authenticate', on: :collection
end
Run Code Online (Sandbox Code Playgroud)
一些客户端 HTML 文件:
<form action='http://localhost:3000/sessions/authenticate' method='post'>
<input type='hidden' name='_method' value='put'>
...
</form>
Run Code Online (Sandbox Code Playgroud)
...在表单提交时:
rails server 输出:
Started POST "/sessions/authenticate" for ::1 at 2018-03-07 11:20:21 +0000
No route matches [POST] "/sessions/authenticate" excluded from capture: DSN not set
ActionController::RoutingError (No route matches [POST] "/sessions/authenticate"):
actionpack (4.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.2.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' …Run Code Online (Sandbox Code Playgroud) 我正在尝试为 Ruby on Rails API 项目实现 Active Storage。我已经根据文档放置了 has_one_attached :picture 。并成功在AWS S3服务上上传图片。现在,当我尝试访问志愿者数据时,它说
Run Code Online (Sandbox Code Playgroud)ActiveStorage::Attachment Load (0.6ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE"active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 8695], ["record_type"], "Volunteer" ["name", "picture"], ["LIMIT", 1]] ActiveStorage::Blob Load (0.4ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [ ["id", 5], ["LIMIT", 1]] [active_model_serializers] 渲染 ActiveModel::Serializer::Null with ActiveStorage::Attached::One (58.41ms) 在 322ms 内完成 500 个内部服务器错误 (ActiveRecord: 48.8)多发性硬化症)
Run Code Online (Sandbox Code Playgroud)SystemStackError (stack level too deep):
我已经通过 Rails 控制台检查了数据,图像保存在图片属性中。志愿者的架构如下 …
rails-api ×10
actioncable ×1
carrierwave ×1
devise ×1
doorkeeper ×1
json ×1
oauth ×1
oauth-2.0 ×1
paperclip ×1
pkce ×1
redis ×1
rspec ×1
ruby ×1