2016-12-27 135 views
0

我在另一个表单中添加搜索功能。我想通过其他表单中的内容进行搜索。在表单中添加搜索功能

Interest_index.html.erb

<%= form_for :interest, url: interest_index_path do |f| %> 
    <p class="col-md-8" align="left"><%= f.submit "Save Schedule", class: "btn btn-primary btn-lg btn-block"%></p> 
    <br> 
    <div class="field form-group col-lg-12"> 
     <h1><%= f.label :interests, "Schedule" %></h1> 
     <br> 
     <div class="row inline"> 
     <%= form_tag interest_index_path, method: :get do %> 
      <p> 
      <div class="col-lg-offset-2 col-lg-7" id="one"> 
      <%= text_field_tag :search, params[:search], class: "form-control", placeholder: "Search" %> 
      </div> 
      <div id="two"> 
      <%= submit_tag "Search", name: nil, class: "btn btn-default" %> 
      </div> 
      </p> 
     <% end %> 
     </div> 

     <% @subjects.each do |subject| %> 
     <div class="field"> 
      <p><h4><%= f.check_box :interests, {:multiple => true}, subject.id, nil %><%= subject.name %></h4></p> 
     </div> 
     <% end %> 
    </div> 
<% end %> 

我有一个更大的形式中的搜索形式,通过该将被选择,然后与主表单提交的受试者进行搜索。我尝试使用基本搜索,因为我是一个新的轨道,但它不工作。

我认为通常在窗体内需要制作嵌套属性,但这是一个搜索表单,我不知道该怎么做,或者这是否可行。

我也有其设置方式是我创建的科目将下利益指数显示了很多。在利益的用户选取自己的利益

这里的主体是控制器和模型

Interests_controller 类InterestController < ApplicationController的

def index 
    if params[:query].present? 
    @subjects = Subject.search(params[:query], load: true) 
    else 
    @subjects = Subject.all.sort_by(&:name) 
    end 
end 

def create 
    current_user.interests = params[:interests].to_json 
    if current_user.save 
    else 
    flash[:danger] = "Something went wrong!" 
    end 
    redirect_to interest_index_path 
    end 

end 

**利率风险模型**

class Interest < ActiveRecord::Base 
searchkick 

include Tire::Model::Search 
include Tire::Model::Callbacks 

def self.search(search) 
    if search 
     where('name LIKE ?', "%#{search}") 
    else 
     scoped 
    end 
end 
end 

主题模式

class Subject < ActiveRecord::Base 
has_many :posts 
has_many :users, through: :posts 
validates_presence_of :name 
validates_uniqueness_of :name 
end 

Subject_controller

class SubjectController < ApplicationController 
    def index 
    @subject = Subject.all.includes(:posts => [:user]) 
    @interests_array = [] 
    if current_user.interests != "null" 
    JSON.parse(current_user.interests).each do |f| 
     if Subject.find(f) 
     @interests_array.push(Subject.find(f)) 
     end 
    end 
    end 
end 
end 

回答

0

明白了,没有必要对嵌套形式。我也在使用错误的模型。