简体   繁体   English

使用Rails中的表单将数据从视图传递到控制器

[英]passing data from view to controller using a form in rails

I have been reading rails documents and guides all day and trying out different tutorials all day but nothing seems to help. 我整天都在阅读Rails文档和指南,并整日尝试各种教程,但似乎无济于事。 I currently have a small app which has a stop table, a bus table and a schedule table. 我目前有一个小型应用程序,其中有一个停止表,一个公交车表和一个时间表。 The schedule table is a rich join between bus and stop table. 时间表表是巴士和车站表之间的丰富连接。 I understand the db and model part of rails clearly compared to the controllers and views. 与控制器和视图相比,我清楚地了解了rail的db和model部分。

My stop table has a id field, a longitude field, and a latitude field. 我的停止表有一个ID字段,一个经度字段和一个纬度字段。

I basically want user to input a value for longitude and latitude and then do a query and find out the stop id by matching the user given longitude and latitude in the stop table. 我基本上希望用户输入经度和纬度的值,然后执行查询并通过匹配停止表中给定的经度和纬度的用户来找出停止ID。

What is the best way to achieve this? 实现此目标的最佳方法是什么? In my opinion i can create a form which will let user enter longitude and latitude and i can store that value in some variable and then do a query. 我认为我可以创建一个表格,让用户输入经度和纬度,并且可以将该值存储在某个变量中,然后进行查询。 I know exactly what i want to do but i don't know how to do it in rails. 我确切地知道我想做什么,但是我不知道该怎么做。 I am very frustrated and i would appreciate any input. 我非常沮丧,我将不胜感激。

My code for form in index.html.erb 我在index.html.erb中的表单代码

<%= form_for :welcome do |f| %>

    <%= f.label :longitude %><br>

    <%= f.text_field :longitude %>
    <br>
    <br>
    <%= f.label :latitude %><br>
    <%= f.text_field :latitude %>


  <p>
    <%= f.submit %>
  </p>
<% end %>

My code for controller 我的控制器代码

class WelcomeController < ApplicationController

  def index
     @long = params[:longitude]
     @lat =  params[:latitude]
  end


end

Please inform me if i am doing something wrong? 如果我做错了事,请通知我? Also how do i proceed from here onwards? 另外,我如何从这里开始? I would really appreciate any help i have been stuck at this same point for the past 8 hours. 在过去的8个小时中,我一直停留在同一时间,我将不胜感激。

First of all, make sure you model is represented in the database, have you run a migration, can you post your schema? 首先,请确保您的模型在数据库中表示,是否已运行迁移,是否可以发布架构?

If you are starting from scratch, you could run this command to get up and running quickly with the model: 如果您是从头开始,则可以运行以下命令以快速启动并运行模型:

bin/rails generate model Location latitude:string longitude:string

bin/rake db:migrate

UPDATE: If you don't need the user Location to persist in the database, you can use ActiveModel (skip the above). 更新:如果您不需要用户Location保留在数据库中,则可以使用ActiveModel (跳过上面的内容)。 In the models folder create a location.rb file and create the ActiveModel: 在models文件夹中,创建一个location.rb文件并创建ActiveModel:

class Location
  include ActiveModel::Model
  attr_accessor :latitude, :longitude
end

Now that Location model is in place, you'll need to save the latitude and longitude using a create method in the controller: 现在位置模型已经就位,您需要使用控制器中的create方法保存纬度和经度:

def create
  @location= Location.new(params[:location])

  @location.save
  redirect_to @location
end

UPDATE: now that you've captured the user location, you want to use this information to compare it to your Stop table in order to find the record that is relevant. 更新:现在您已经捕获了用户位置,您想使用此信息将其与Stop表进行比较,以找到相关的记录。 Here, you'll want to do this work using resque (or other queue) or by making a call to a third-party API like Google maps for example, or use whatever algorithm you've come up with. 在这里,您将要使用resque(或其他队列)或通过调用第三方API(例如Google Maps)或使用您想出的任何算法来完成此工作。

You also want to make sure that you have routes, the default CRUD routes can be achieved by: 您还需要确保您具有路由,可以通过以下方式实现默认的CRUD路由:

Rails.application.routes.draw do

  resources :location
end

finally in your view (note I'm using the model name location instead of welcome): 最终在您看来(请注意,我使用的是模型名称位置而不是欢迎使用):

<%= form_for :location do |f| %>
  <p>
    <%= f.label :latitude %><br>
    <%= f.text_field :latitude %>
  </p>

  <p>
    <%= f.label :longitude %><br>
    <%= f.text_area :longitude %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

If you want something with a little bit more features, you should try geokit-rails gem , this will let you query stops by coordinates, and get results even if the coordinates aren't exactly, right, it will fetch the closest stops. 如果您想要一些具有更多功能的东西,则应该尝试使用geokit-rails gem ,这将使您可以按坐标查询停靠点,即使坐标不完全正确也可以获取结果,正确的是,它将获取最近的停靠点。

  • First check the gem page and check how to install the gem. 首先检查gem页面,并检查如何安装gem。
  • Then add the mappable command in your stop model 然后在您的停止模型中添加mappable命令

     class Stop < ActiveRecord::Base acts_as_mappable end 

    note: check the options for acts_as_mappable cause you'll need to change some stuff, like default distance unit and default lat and lng column names 注意:请检查acts_as_mappable 的选项 ,因为您需要更改一些内容,例如默认距离单位以及默认latlng列名称

  • Then now you could do a query in your controller like this 然后,您可以像这样在控制器中进行查询

     class WelcomeController < ApplicationController def index lng = params[:longitude] lat = params[:latitude] Stop.within(5, origin: [lat, lng] end end 

    This will find all the bus stops that are within 5 miles (which is the default distance unit, can be changed to km) from the point you provided in the search 这将查找距离您在搜索中提供的点相距5英里(默认距离单位,可以更改为km)内的所有公交车站

note : The params will probably be scoped inside a :welcome key, so it might be better to whitelist them using strong parameters 注意 :这些参数可能会在:welcome键中限定范围,因此最好使用强参数将其列入白名单

def bus_stop_params
  params.require(:welcome).permit(:latitude, :longitude)
end

If I can suggest something: watch this Railscast http://railscasts.com/episodes/273-geocoder?view=asciicast 如果我可以提出建议,请观看此Railscast http://railscasts.com/episodes/273-geocoder?view=asciicast

This will help you achieve what you are looking for it a simpler manner than the route you currently are going down. 这将帮助您以比当前要走的路线更简单的方式来实现所需的功能。

Basically what this episode entails is how to use a gem to convert place names to coordinates and vice-versa and even convert IP addresses to street addresses. 基本上,此情节涉及的是如何使用gem将地名转换为坐标,反之亦然,甚至将IP地址转换为街道地址。 The Geocoder gem can also find nearby locations with their distance and bearing and has many other useful features. Geocoder宝石还可以通过其距离和方位找到附近的位置,并具有许多其他有用的功能。

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

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