Giter Site home page Giter Site logo

Comments (7)

PENGZhaoqing avatar PENGZhaoqing commented on September 4, 2024

这是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.

tangmaomao16 avatar tangmaomao16 commented on September 4, 2024

老师,我猜测你给的代码的作用是:用户把文本输入到搜索框中,点击搜索按钮后,输入的值会保存在:query符号中,且会执行<%= form_tag houses_path, method: 'get', role: "search" do %>,即调用控制器中的houses_controller内的动作(方法)search,然后网页会变为houses_path的URI的网页。
请批评指教。

from courseselect.

PENGZhaoqing avatar PENGZhaoqing commented on September 4, 2024

你猜的没错,提交这个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.

tangmaomao16 avatar tangmaomao16 commented on September 4, 2024

我的搜索结果是0条数据记录
image

image

from courseselect.

tangmaomao16 avatar tangmaomao16 commented on September 4, 2024

写入代码步骤如下。

  1. 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 %>
  1. 查看routes.rb文件
  resources :courses do
    member do
      get :select
      get :quit
      get :openc
      get :close
     
    end
    collection do
      get :list
      
    end
  end
  1. 在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
  1. 在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.

PENGZhaoqing avatar PENGZhaoqing commented on September 4, 2024
 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.

PENGZhaoqing avatar PENGZhaoqing commented on September 4, 2024

你的代码存在冗余:

   @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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.