2
Space
이라는 개체에 대한 검색 양식을 만들려고합니다. 나는 if
과 elsif
을 많이 사용하려고 시도 했으므로 Ransack을 확인하기 시작했다. 사람이 작동하도록 만들면 위대한 것 같습니다.Ransack이 매개 변수를 고려하지 않음
문제는 내 첫 페이지에 양식이 있습니다. home#index
. 모두가 결과에 표시됩니다 같은 형태가 같은 페이지에 자습서, 나는 그래서 여기에 내 코드 ...
홈 (index.html.erb)
<div class="col-md-12 text-center">
<%= search_form_for @q, class: "form-inline" do |f| %>
<div class="input-append">
<%= f.label :city %>
<%= f.search_field :city %>
<div class="row">
<button class="btn" type="submit"><i class="fa fa-search"></i></button>
</div>
</div>
<% end %>
</div>
는 대신
spaces#index
에 가고 싶다 HomeController.rb
class HomeController < ApplicationController
def index
@q = Space.search(params[:q])
@spaces = Space.all
@spaceimages = Spaceimage.all
@spacetypes = @spaces.select(:spacetype).distinct
end
end
SpacesController.rb
def index
@q = Space.search(params[:q])
@spaces = @q.result
end
그리고이 승입니다 모자 서버가 가져옵니다
Started GET "/spaces?utf8=%E2%9C%93&q%5Bcity%5D=Gothenburg" for 127.0.0.1 at 2015-02-04 21:32:44 +0100
Processing by SpacesController#index as HTML
Parameters: {"utf8"=>"✓", "q"=>{"city"=>"Gothenburg"}}
Space Load (0.2ms) SELECT `spaces`.* FROM `spaces`
Rendered spaces/index.html.erb within layouts/application (1.2ms)
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Completed 200 OK in 52ms (Views: 50.7ms | ActiveRecord: 0.4ms)
는 그래서 PARAMS이 있지만
@spaces.search
(또는
.ransack
)는 쿼리 아무것도하지 않습니다. 왜?
는'f.search_field 같은 형태 헬퍼의 일부를 사용하여 노력했다. 또한'space' 모델에'self.ransackable_attributes'라는 임 플리 션이 있습니까? (괜찮지 만 그렇다면 ransack이 검색 매개 변수를 무시하도록 할 수 있으므로 게시하십시오.) – engineersmnky
사실 그것은 정확히 무엇입니까! _cont 또는 similiar이 search_field에 추가되어야합니다. 그렇지 않으면 취하지 않습니다. 감사합니다. 한 무리! :) @engineersmnky – Doddlin