简体   繁体   中英

ransack search is not working with partials

I have tried using the 'ransack' search with my application, it works as long as it is in the index view of the controller, it does not work when I try putting the same code in partials. The partial has the header with a search field in it

My index action of expert controller looks like this:

def index
@search = Expert.search(params[:q])
@experts = @search.result
end

The code I have used in the index view of expert is

<form class="navbar-form navbar-left" role="search">                                        
<%= search_form_for @search do |f|  %>
  <div class="form-group">
      <div class="field">
                <%=f.text_field:Location_or_Description_or_Industry_or_Current_Organization_or_Current_Position_cont, :class => "form-control" %>
      </div>
  </div>
  <%= f.submit "Search" , :class => "btn btn-default icon-search"  %> 
<% end %>
</form> 

I am trying to use this code in partial so that I can search from any given page in my app. The error I get when I do so is

ArgumentError in Praclycore#index

Showing c:/repos/pracly/app/views/layouts/_header.html.erb where line #24 raised:
No Ransack::Search object was provided to search_form_for!

<li class="home"><%= link_to image_tag("/img/home.png"), root_path, :class=>"home" %></li>

<form class="navbar- form navbar-left" role="search">                                   
    <%= search_form_for @search do |f|  %>
    <div class="form-group">
    <div class="field">
    <%= f.text_field :Location_or_Description_or_Industry_or_Current_Organization_or_Current_Position_cont, :class => "form-control" %>




Trace of template inclusion: app/views/layouts/application.html.erb

Rails.root: c:/repos/pracly

I am new to ruby on rails, it would be highly appreciated if someone can tell me how can I use ransack with partials and what additional code I need to supply for this to work .

@search实例变量在除Expert#search之外的任何上下文中均不可用-您将需要设置该变量(或者更好的方法是使用decent_exposure gem)并在ApplicationController级别公开它或在before过滤器中创建它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM