简体   繁体   English

Ruby on Rails为表创建新记录

[英]ruby on rails create new record for a table

The code below create a new project in database record.It takes in parameters like job_location and project_title,contact_person. 下面的代码在数据库记录中创建一个新项目。它接受诸如job_location和project_title,contact_person之类的参数。

 def new
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @project }
    end
  end

Now I want to convert the job_location to latitude and longitude use other web-service(input address,out put a pair of numbers) and store into database. 现在,我想使用其他Web服务(输入地址,输出一对数字)将job_location转换为纬度和经度并存储到数据库中。 The convert part is done. 转换部分完成。

where should I add code to transfer this address to number and store in database? 我应该在哪里添加代码以将该地址传输到数字并存储在数据库中?

I have tried add 我尝试添加

@project.update_attributes(lat: 10 ,lng: 11)

At end of new function,but doesn't work. 在新功能结束时,但不起作用。

I don't know how to retrieve data (just inputted by user) 我不知道如何检索数据(仅由用户输入)

don't know how to put converted data into database. 不知道如何将转换后的数据放入数据库。

where and what code should add to achieve this? 应该在哪里添加什么代码来实现这一目标?

I think you should use Migration to add lat and lng to your database. 我认为您应该使用Migrationlatlng添加到数据库中。

If you can't change your database, maybe you just convert lat and lng to your struct and then save it into database. 如果您不能更改数据库,则可能只是将latlng转换为您的结构,然后将其保存到数据库中。

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

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