我目前正在使用 gem google-maps-for-rails 开发一个回收应用程序,以显示带有垃圾箱/回收箱位置等的地图。我有一个带有名称和地址作为属性的垃圾.rb 模型。gem 允许您获取属性,即地址并将其转换为地图上的标记和纬度/经度。
我正在尝试实现一个狮身人面像功能,假设用户想要回收“论文” - 这将在我表中的类别列下。我索引了类别列。我希望当用户发布“论文”时,只有具有相同类别的地址才会在地图上显示其标记。
一切都得到了正确的索引。我没有收到任何错误,但什么也没发生。
我无法将当前实例传递给表单?在昨晚考虑了这个之后,我正在编辑这个。
这是我的垃圾控制器.rb,只关注索引方法
class TrashesController < ApplicationController
# GET /trashes
# GET /trashes.json
def index
@trashes = Trash.search(params[:search])
end
end
# GET /trashes/1
# GET /trashes/1.json
def show
@trash = Trash.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @trash }
end
end
# GET /trashes/new
# GET /trashes/new.json
def new
@trash = Trash.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @trash }
end
end
# GET /trashes/1/edit
def edit
@trash = Trash.find(params[:id])
end
# POST /trashes
# POST /trashes.json
def create
@trash = Trash.new(params[:trash])
respond_to do |format|
if @trash.save
format.html { redirect_to @trash, notice: 'Trash was successfully created.' }
format.json { render json: @trash, status: :created, location: @trash }
else
format.html { render action: "new" }
format.json { render json: @trash.errors, status: :unprocessable_entity }
end
end
end
# PUT /trashes/1
# PUT /trashes/1.json
def update
@trash = Trash.find(params[:id])
respond_to do |format|
if @trash.update_attributes(params[:trash])
format.html { redirect_to @trash, notice: 'Trash was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @trash.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trashes/1
# DELETE /trashes/1.json
def destroy
@trash = Trash.find(params[:id])
@trash.destroy
respond_to do |format|
format.html { redirect_to trashes_url }
format.json { head :no_content }
end
end
垃圾.rb 模型
class Trash < ActiveRecord::Base
attr_accessible :address, :name, :category,
acts_as_gmappable
def gmaps4rails_address
"#{address}"
end
def gmaps4rails_infowindow
"#{self.address}"
end
end
index.html.erb
<h1>Listing Boston/Cambridge trash bin locations</h1>
<%= gmaps4rails(@json) %>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Category</th>
<th></th>
<th></th>
</tr>
<% @trashes.each do |trash| %>
<tr>
<td><%= trash.name %></td>
<td><%= trash.address %></td>
<td><%= trash.category %></td>
<td><%= link_to 'Edit', edit_trash_path(trash) %></td>
<td><%= link_to 'Destroy', trash, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>
<%= form_tag trashes_path, method: :get do %>
<div class="field">
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</div>
<% end %>
我已将 index.html.erb 更改为此以测试 sphinx 功能。
<%= link_to 'New Boston Solar Powered Trash Can Location', new_trash_path %>
<%= form_tag trashes_path, method: :get do %>
<div class="field">
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
</div>
<% end %>
它正在搜索,我使用 rails 控制台,并且可以搜索模型。但是它没有显示任何内容。