我想从数据API创建对象。首先,用户将必须输入其模拟号码。输入注册信息后,他将在视图视图中返回以下数据
@vehicle = Vehicle.new
@vehicles = []
vehicle_number = params['immatricule'].capitalize
vehicles.each do |vehicule|
data_api = CarRegistrationFrance.Lookup(#{"vehicle_number"},"username","password")
Api_data response:
=> {"Description"=>"RENAULT CLIO IV", "RegistrationYear"=>"2017", "CarMake"=>{"CurrentTextValue"=>"RENAULT"}, "CarModel"=>{"CurrentTextValue"=>"CLIO IV"}, "EngineSize"=>{"CurrentTextValue"=>"4"}, "FuelType"=>{"CurrentTextValue"=>"DIESEL"}, "MakeDescription"=>{"CurrentTextValue"=>"RENAULT"}, "ModelDescription"=>{"CurrentTextValue"=>"CLIO IV"}, "Immobiliser"=>{"CurrentTextValue"=>""}, "IndicativeValue"=>{"CurrentTextValue"=>0}, "DriverSide"=>{"CurrentTextValue"=>""}, "BodyStyle"=>{"CurrentTextValue"=>"BERLINE 5 PORTES"}, "RegistrationDate"=>"2017-10-30","ExtendedData"=>{"anneeSortie"=>"2017", "boiteDeVitesse"=>"", "carburantVersion"=>"D", "carrosserieVersion"=>"", "classeSra"=>"K", "libVersion"=>"1.5 DCI 90 EDITION ONE EDC", "libelleModele"=>"CLIO IV", "marque"=>"RE", "modele"=>"88", "produit"=>"", "puissance"=>"4", "version"=>"", "cleCarrosserie"=>"", "groupeSra"=>"30", "nbPlace"=>"5", "datePremiereMiseCirculation"=>"30102017", "questionBatterie"=>"", "electrique"=>"", "genre"=>"", "typeVehicule"=>"", "numSerieMoteur"=>"VF15RBJ0D58888591", "valeurANeufSRA"=>"", "niveauRisqueVol"=>"", "protectionConstructeur"=>"", "puissanceDyn"=>"", "segmentVeh"=>""}}
@vehicles << data_api
vehicle.description => data_api["Description"]
vehicle.annee => data_api["RegistrationYear"]
def vehicle_params
params.require(:voiture).permit(:immatricule, :description, :annee)
end
一旦用户输入他的注册号码,我想加入
data_api ["Description"] << vehicle ["description"]
将数据附加到车辆对象的属性
data_api ["RegistrationYear"] << vehicle ["year"]
/ user [:id] / vehicle / new其示例
<%= form_tag("/vehicle", method: "post") do %>
<%= label_tag(:immatricule, "") %>
<%= text_field_tag(:immatricule) %>
<%= submit_tag("create") %>
/ user [:id] /车辆/显示示例
<%= @vehicle.api_data["Description"] %>
<%= @vehicle.api_data["RegistrationYear"] %>
我尝试了几种操作,但找不到解决方案。
答案 0 :(得分:0)
因此,这种感觉就像您来自背景而没有CRUD
(Create, Read, Update, Delete
)的意识...基于您未列出要实现此目标的控制器等(关键回答您问题的任何回复)。
根据问题2/22 @ 06:18 am -600的状态,这里有一些快速注释...
您将需要逐步入门-RailsGuides。
第二,我认为从您列出的URL中开始-您具有嵌套路线,并且正在使用匹配的嵌套模型。答案会更改我建议的控制器/动作/路由-因此,在尝试理解该答案之前,请务必先弄清楚您是否不是...
所有这些都是猜测工作,因为我们缺少实现的详细信息(route.rb文件,如果这是您作为管理员正在调用的脚本,或者每个用户都在运行更新,如果有的话完全可以)。
完成您要问的问题应该是...
“我应该使用哪个控制器和控制器操作来更新注册信息?” (通常显示为controller#action
)
CRUD
的答案是update
(将其称为Edit
以显示视图,而将其称为update
以执行)。
您的问题之所以会让人失望,是因为Rails Way(CRUD
)已经具有控制器的基本代码以及该控制器要更新字段的操作。您不必自己创建对象-所有永久的数据对象都应该是Rails模型对象。
在您的情况下,Vehicle
中的app/models/vehicle.rb
模型是我的猜测。您可能选择将{@ 1}}文件中的路由嵌套为...
config/routes.rb
哪条路径/网址为Rails.application.routes.draw do
resources :users do
resources :vehicle
...
。
这是假设...
"/{:user_id}/{:vehicle_id}/{controller action}"
操作(Rails将此称为read
)。它也已经在控制器操作中进行了编码以进行更新。在您的情况下,这很可能是... Show
,它可能具有User#show
模型中的accepts_nested_attributes_for :RegistrationYear
以及:Description
,请参考User
个您已经在使用的模型。
对于另一种情况...您只是包装另一个正在更新的API,而您的rails应用程序没有任何内容...
您需要一个用户实体或身份验证...也许您正在从所引用的API中获取omni_auth或其他安全登录令牌...因此您的步骤...
Vehicle
应该具有指向User#Show
的链接,或者Vehicle#Update
视图应该包括带有用户和车辆ID ...的字段的表格,该表格已经填写为控制器执行该API查询以提取所有信息并具有两个要更新的字段。User#Show
负责显示表单。 如果您只是将API与您自己的rails应用程序包装在一起,我建议您做一个标准的目标文件...
controller#action
所以...您现在有了API处理程序对象...
在使用中,您可以在controller#action中调用它……让我们说出User#Show,它将提交重定向到# depending on where you put this you might need to load it yourself
app/lib/other_api_connection.rb
# rails prefers this format as part of ruby & it's class autoloading
class OtherAPI < CarRegistrationFrance
attr_accessor :registration, :description
def initialize(search_immat, user, pass, api_url = {whatever currently})
...
@user = user
@pass = pass
@record = look_up
@api_url = api_url
end
def look_up(search_immat)
...
Lookup("#{search_immat}", @user, @pass)
end
def update_api
begin
... {you haven't shown us the update API url} ...
rescue {some error from api}
return "{some error message}"
end
end
end
或User#Update
Vehicle#Update
注意-我建议创建一个API接口类/对象,因为您随后可以从控制器或操作中调用代码,并且仅在api url发生更改等情况下才需要在一个位置进行更改...但是您可能必须在每个控制器代码的顶部class Vehicle < ActionController::Base
...
def update
...
# normal stuff probably needs to be deleted if you aren't saving to your application db THEN updating API (aka just wrapping an API - which I'm assuming you are doing since you haven't said otherwise & it's less work).
# also you could pass the object as a parameter in the submit link - but that's not really CRUD per say
@user = {whatever user is}
api = OtherAPI.new(user, pass)
# you might have to permit this as parameter to pull it out
@response_code = api.update_api(:RegistrationYear, :Description) # or whole vehicle record if the API requires
if @response_code == {whatever valid code is from that API documentation}
format.html { redirect_to @vehicle, notice: 'Vehicle was successfully updated.' }
else
format.html { render :edit }
format.json { render json: @vehicle.errors, status: :unprocessable_entity }
end
... rest of controller actions
end
。
答案 1 :(得分:0)
您好,谢谢您的帮助
i使用devise对用户进行身份验证
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.float "longitude"
t.float "latitude"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "avatar_file_name"
t.string "avatar_content_type"
t.bigint "avatar_file_size"
t.datetime "avatar_updated_at"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
create_table "vehicles", force: :cascade do |t|
t.string "immatricule"
t.string "description"
t.string "marque"
t.string "date_circulation"
t.string "fuel_type"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_vehicles_on_user_id"
end
add_foreign_key "vehicles", "users"
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :trackable
has_many :vehicles
class Vehicle < ApplicationRecord
belongs_to :user
end
我需要以下路线:
user_vehicles GET /users/:user_id/vehicles(.:format) vehicles#index
POST /users/:user_id/vehicles(.:format) vehicles#create
new_user_vehicle GET /users/:user_id/vehicles/new(.:format) vehicles#new
edit_user_vehicle GET /users/:user_id/vehicles/:id/edit(.:format) vehicles#edit
user_vehicle GET /users/:user_id/vehicles/:id(.:format) vehicles#show
PATCH /users/:user_id/vehicles/:id(.:format) vehicles#update
PUT /users/:user_id/vehicles/:id(.:format) vehicles#update
DELETE /users/:user_id/vehicles/:id(.:format) vehicles#destroy
但我正在使用设备
在我的控制器中,我需要构建 带有响应API的车辆,我需要将数据保存在数据库中 从数字无数 形式,新车用户输入number_immatricule “ immatricule” =形成新的车辆[:immatricule]用户 表单获取data_api
def build_vehicle_user_from_api_data
"description" == data_api["description"]
"marque" == data_api["CarMake"]
"date_circulation" == data_api["RegistrationDate"]
"fuel_type" == data_api["fuel_type"]
end
我正在寻找如何在控制器用户或车辆中做到这一点