简体   繁体   English

使用Ruby跟踪更新的信息?

[英]Using Ruby to track updated information?

The script I am trying to build works like this. 我试图构建的脚本就是这样的。

  1. User directs application to a webpage containing a number that represents total # of miles traveled 用户将应用程序定向到包含代表行驶里程总数的数字的网页
  2. Application saves that number 应用程序保存该号码
  3. User comes back 30 minutes later after traveling 用户在旅行30分钟后回来
  4. Application returns miles per hour 申请返回每小时英里数

Right now I can do step 1. I'm struggling to get my application to save the number the user obtained in step 1. 现在我可以做第1步。我很难让我的应用程序保存用户在步骤1中获得的数字。

Ideally, this should work like a timer. 理想情况下,这应该像计时器一样工作。 The user starts the timer, goes for a drive, comes back, stops the timer, and his average speed is returned to him. 用户启动计时器,驱动器,返回,停止计时器,并将他的平均速度返回给他。

Is Ruby and Rails enough? Ruby和Rails足够吗? Or will I need some sort of AJAX or JavaScript? 或者我需要某种AJAX或JavaScript?

Ruby on Rails is enough. Ruby on Rails就足够了。 You can keep the starting time as data in step 1, and call that in step 3 or 4. 您可以将开始时间保留为步骤1中的数据,并在步骤3或4中调用该数据。

I think you're getting confused between how Rails, JS & Ajax work 我认为你对Rails,JS和Ajax的工作方式感到困惑

Rails is backend; Rails是后端; will handle your db & requests. 将处理您的数据库和请求。 JS / Ajax will process any front-end requests made directly by the client in the browser JS / Ajax将处理客户端在浏览器中直接发出的任何前端请求


Make Another Model 制作另一种模式

What you're asking is actually quite simple if you break it into modular pieces 如果你把它分解成模块化的部分,你所要求的实际上非常简单

  1. User doesn't have many miles; User没有多少英里; a User's Trip has many miles User's Trip有很多里程
  2. Therefore, I'd introduce a new model call Trip 因此,我将介绍一个新的模型调用Trip
  3. Each Trip will have many miles 每次Trip都会有很多里程
  4. You can create a new join model call TripMiles which you'll be able to store the miles in 您可以创建一个新的连接模型调用TripMiles ,您可以将其存储在里程中
  5. Once you've got the miles in this model, you can use the created_at times to see how many miles per hour 在此模型中获得里程后,您可以使用created_at次数查看每小时的里程数

Here are some examples: 这里有些例子:

#app/models/user.rb
Class User < ActiveRecord::Base
    has_many :trips
end

#app/models/trip.rb
Class Trip < ActiveRecord::Base
    belongs_to :user
    has_many :miles, :class_name => "TripMile"
end

#app/models/trip_mile.rb
Class TripMile < ActiveRecord::Base
    belongs_to :trip
end

trips
id | user_id | created_at | updated_at 

trip_miles
id | trip_id | created_at | updated_at

This will allow you to set the trip as a specific record, and then add as many miles to it through the trip_miles model. 这将允许您将trip设置为特定记录,然后通过trip_miles模型trip_miles添加尽可能多的里程。 How you add these will depend on your front-end, but it will all be Rails 你如何添加这些将取决于你的前端,但它将全部是Rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM