Comments (7)
这是HousePricing应用的搜索框
https://github.com/PENGZhaoqing/HousePricing/blob/master/app/views/layouts/_navside.html.erb
<%= form_tag houses_path, method: 'get', role: "search" do %>
<div class="input-group">
<%= text_field_tag 'query', params[:query], class: "form-control inpu", placeholder: "搜索关键字" %>
<div class="input-group-btn">
<button class="btn btn-default btn" type="submit"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
<% end %>
from courseselect.
老师,我猜测你给的代码的作用是:用户把文本输入到搜索框中,点击搜索按钮后,输入的值会保存在:query符号中,且会执行<%= form_tag houses_path, method: 'get', role: "search" do %>
,即调用控制器中的houses_controller内的动作(方法)search,然后网页会变为houses_path的URI的网页。
请批评指教。
from courseselect.
你猜的没错,提交这个form后会到houses控制器的index方法,通过search_params获得参数值,然后调用了house模型的search方法获得搜索结果,返回的视图代码不变
控制器方法
def index
@houses=House.search(search_params).paginate(:page => params[:page], :per_page => 20)
end
模型中的方法:
def self.search(query)
unless query.blank?
if query.to_i.to_s==query
houses=House.where('build_time = :search OR area = :search', search: query)
else
houses=House.where('community LIKE :search OR street LIKE :search OR floor LIKE :search OR room_shape LIKE :search', search: "%#{query}%")
end
else
houses= House.all
end
return houses
end
from courseselect.
from courseselect.
写入代码步骤如下。
- views/courses/list.html文件中写入搜索框视图代码
<%= form_tag courses_path, method: 'get', role: "search" do %>
<div class="input-group">
<%= text_field_tag 'query', params[:query], class: "form-control inpu", placeholder: "搜索关键字" %>
<div class="input-group-btn">
<button class="btn btn-default btn" type="submit"><i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
<% end %>
- 查看routes.rb文件
resources :courses do
member do
get :select
get :quit
get :openc
get :close
end
collection do
get :list
end
end
- 在courses_controller.rb文件的list代码区中写入调用搜索的方法
def list
@course=Course.all
@course= @course-current_user.courses
@openarray = []
@course.each do |course|
@openarray.push(course) if course.open
end
@[email protected]
@course=@openarray
@course=Course.search(search_params)
end
- 在models/course.rb文件中写出Course.search(search_params)方法的定义
def self.search(query)
unless query.blank?
if query.to_i.to_s==query
courses=Course.where(open==true)
courses=Course.where('name = :search ', search: query)
else
courses=Course.all.where(open==true)
end
end
return courses
end
5.检查迁移文件,确认Course表有:name这一字段
class CreateCourses < ActiveRecord::Migration
def change
create_table :courses do |t|
t.string :name
t.string :course_code
t.string :course_type
t.string :teaching_type
t.string :exam_type
t.string :credit
t.integer :limit_num
t.integer :student_num, default: 0
t.string :class_room
t.string :course_time
t.string :course_week
t.belongs_to :teacher
t.timestamps null: false
end
end
end
不懂问题出在哪?
from courseselect.
if query.to_i.to_s==query
houses=House.where('build_time = :search OR area = :search', search: query)
else
houses=House.where('community LIKE :search OR street LIKE :search OR floor LIKE :search OR room_shape LIKE :search', search: "%#{query}%")
end
我之所以用 if query.to_i.to_s==query
是因为我的build_time和area字段都是int型,int型的跟string类型的where方法不一样,所以query.to_i.to_s==query能判断query这个值是否int型
另外:
courses=Course.all.where(open==true)
与courses=Course.all.where(:open=true)
,不知道你有没有注意到语法错误
from courseselect.
你的代码存在冗余:
@openarray = []
@course.each do |course|
@openarray.push(course) if course.open
end
@[email protected]
@course=@openarray
@course=Course.search(search_params)
先选出了所有开放的课程,然后又在search方法里又有相同功能的代码
from courseselect.
Related Issues (20)
- 关于在rails视图中使用二维数组 HOT 2
- redirect_to HOT 1
- 请问如何将数据导入到数据库中 HOT 2
- 导出课程列表的时候提示路由错误 HOT 2
- 关于删除选课的参数问题 HOT 8
- github在pull request时出现conflict HOT 1
- cloud9上执行rake db:migrate时的问题 HOT 1
- 查询数据库的问题 HOT 1
- 下载excel文件自动跳转微软url问题
- form表单submit按钮无法提交,除非刷新一次 HOT 3
- sudo service postgrsql start 出现问题 HOT 1
- heroku 部署的问题 HOT 1
- rake db:seed rake aborted! Email has already been taken HOT 1
- gem install nokogiri -v '1.6.8' HOT 1
- 执行rake test 报错 HOT 4
- heroku上部署以后无法发送邮件 HOT 1
- 在heroko无法成功branch HOT 5
- An error occurred while installing pg (0.18.4) HOT 8
- bundle instll error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from courseselect.