Rails 4 - 使用 Geocoder 在位置输入字段中自动填写 IP 地址位置
Rails 4 - Automatically fill in IP address location on the locations input field using Geocoder
我有这个表格:
= simple_form_for @post, validate: true do |f|
.edit-form
= f.input :title
= f.input :location
= f.input :price
= f.button :submit
输入表单显然是人们放在他们位置的空白文本框。
我目前在我的应用程序中有 Geocoder,我想做的是用用户的位置预填充位置文本框。这样做需要这行代码:
= request.location.city
= request.location.state
我想在我的位置文本框中预先填写。我试过这样做:
= f.input :location, value: (#{request.location.city}, #{request.location.state})
最终产品应该会自动填写信息,如下所示:
这没有用。我如何插入它来实现这个目标?
不要在值周围加上括号,而是使用引号使它成为 value: "#{request.location.city}, #{request.location.state}"
。
其他答案都是错误的。正确的做法是:
= f.input :location, input_html: { value: "#{request.location.city}, #{request.location.state}"}
有必要包括
input_html: { }
如果您想添加任何其他 HTML 属性。
我有这个表格:
= simple_form_for @post, validate: true do |f|
.edit-form
= f.input :title
= f.input :location
= f.input :price
= f.button :submit
输入表单显然是人们放在他们位置的空白文本框。
我目前在我的应用程序中有 Geocoder,我想做的是用用户的位置预填充位置文本框。这样做需要这行代码:
= request.location.city
= request.location.state
我想在我的位置文本框中预先填写。我试过这样做:
= f.input :location, value: (#{request.location.city}, #{request.location.state})
最终产品应该会自动填写信息,如下所示:
这没有用。我如何插入它来实现这个目标?
不要在值周围加上括号,而是使用引号使它成为 value: "#{request.location.city}, #{request.location.state}"
。
其他答案都是错误的。正确的做法是:
= f.input :location, input_html: { value: "#{request.location.city}, #{request.location.state}"}
有必要包括
input_html: { }
如果您想添加任何其他 HTML 属性。