我是rails的新手,读过有关下拉菜单的文档,但仍在苦苦挣扎。
我有一个如下的数据库:
Table "public.lakeaddresses"
Column | Type | Modifiers | Storage | Stats target | Description
---------+-----------------------+-----------+----------+--------------+-------------
address | character varying(40) | | extended | |
city | character varying(25) | | extended | |
state | character varying(20) | | extended | |
zip | integer | | plain | |
county | character varying(25) | | extended | |
lake | character varying(30) | | extended | |
lakeid | integer | | plain | |我想创建一个填充湖泊名称的下拉列表,它将是列"lake“
视图/map/index.html.erb
<head>
<script src='https://api.mapbox.com/mapbox-gl-js/v0.26.0/mapbox-gl.js'>
</script>
<link href='/home/garrett/lakemag/app/assets/stylesheets/lakemap.css' rel='stylesheet' />
</head>
<body>
<div id='map' style='margin:0 auto; width: 1024px; height: 768px;'>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZ3JhZmZpbmR1c3RyaWVzIiwiYSI6ImNpdXAxY2pycjAxeGoyb251dDY2emNqeXgifQ.YtJkR5Elp-oq3FaMTUlXyw';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/graffindustries/ciursnqrv009u2js5cpedcnsr'
});
</script>
</div>
</br></br>
<center>
<% form_tag(method: "get") do %>
<% lake_array = Lake.all.map { |lake| [lake.name] } %>
<= select_tag 'Lake Name', options_for_select(lake_array) %>
<% end %>
</center>
</body>控制器/map_CONTRONTER.rb
def new
@lake = Lake.all收到错误:
ActionView::Template::Error (uninitialized constant ActionView::CompiledTemplates::Lake
18:
19: <center>
20:
21: <% form_tag(method: "get") do %>
22: <% lake_array = Lake.all.map { |lake| [lake.name] } %>
23: <= select_tag 'Lake Name', options_for_select(lake_array) %>
24: <% end %>
25:
26: </center>
F, [2016-11-02T06:22:04.619269 #7096] FATAL -- : [f4086edc-8de8-4b36-9b81-4a20efbde333] app/views/map/index.html.erb:21:in `_app_views_map_index_html_erb___3432893749292109102_24860500'发布于 2016-11-02 14:06:20
而不是做<% options = options_from_collection_for_select(@lake, 'lake') %>
尝试以这种方式建立您的数组。
<% lake_array = Map.all.map { |map| [map.lake] } %>
<%= select_tag 'Lake Name', options_for_select(lake_array) %>请注意,除非您使用的是form_for,否则不能使用f.select,而必须使用select_tag
更新:您收到了错误,因为lake派生自map表。如果您有一个包含表列name的lake.rb模型,则可以在rails控制台中执行以下操作。
lake = Lake.first
lake_name = lake.name但是现在,因为你不能像这样访问Lake.method,所以你必须通过你的Map来访问它。例如,为了在rails控制台中访问您的一个湖泊名称,您必须这样做。
map = Map.first
lake_name = map.lake
#this would return the lake that is tied to the first map object希望这能消除你的疑虑,并解释为什么你会收到这个错误。
https://stackoverflow.com/questions/40372385
复制相似问题