我开始使用rails并尝试合并一个简单的搜索,但我只是到目前为止。 字符串看起来不错,但似乎没有执行到结果页面。当我尝试包含restful资源时,似乎存在一些问题,没有方法,行动缺失或路由都会崩溃。
我希望在一个页面上搜索(搜索),并将结果填充到另一个结果页面(地图)。 两者都属于PagesController,并且是其中的动作。
该表称为城镇,用户条目和CRUD区域由TownsController控制,并与设计User_id有关联。
然后页面控制器有搜索,地图,关于,联系页面。
class PagesController < ApplicationController
def index
end
def search
@towns = Town.search(params[:search])
end
def page
end
def map
end
end
--------------------------
class Town < ActiveRecord::Base
geocoded_by :name
after_validation :geocode
belongs_to :user
def self.search(search)
if search
search_condition = "%" + search + "%"
where(['townName LIKE ? OR townDescription LIKE ?', search_condition, search_condition])
end
end
-------------------------
views
search.html.erb
<div class="form-group">
<%= form_tag(pages_map_path , method: "get") do %>
<p>
<%= text_field_tag :search, params[:search], class: 'search-text' %>
<%= submit_tag "Search", :name => nil, class: 'btn btn-primary btn-lg'%>
</p>
<% end %>
results --> to map page
map.html.erb
<ul>
<% @towns.each do |town| %>
<li><%= link_to town.name,
:action => 'map', :id => town.id %></li>
<% end %>
</ul>
---------------------------
routes
Rails.application.routes.draw do
devise_for :users
get "pages/search"
get "pages/index"
get "pages/contact"
get "pages/about"
get "pages/map"
get "pages/page"
match ':controller(/:action(/:id))', :via => [:get, :post]
root 'pages#search'
end
答案 0 :(得分:0)
解决方案:
搜索查询中的列名不正确
townName LIKE ? OR townDescription should be = name LIKE ? OR description
更新结果页面调用:
<ul>
<% @towns.each do |town| %>
<h2><li><%= link_to @name, controller: 'towns', :action => 'show', :id => town.id %></li></h2>
<% end %>
</ul>