2016-10-28 49 views
0

我需要對學校的控制器,它會顯示搜索結果,如果提供的參數,但我無法得到它的工作的放映視圖搜索表單:搜索與洗劫如果提供的參數

schools_controller

def show 
    find_city 
    find_school 

    @search = @school.users.ransack(params[:q]) 

    if (params.has_key?(:q)) 
     @users = @search.result 
    else 
     @users = @school.users.all 
    end 
end 

路線

get "/cities/:city_id/school/:school_id" => "users#index", as: "users" 
    get "/cities/:city_id/school/:school_id/user/new" => "users#new", as: "new_user" 
    post "/cities/:city_id/school/:school_id" => "users#create" 
    get "/cities/:city_id/school/:school_id/user/:id" => "users#show", as: "user" 
    get "/cities/:city_id/school/:school_id/user/:id/edit" => "users#edit", as: "edit_user" 
    patch "/cities/:city_id/school/:school_id/user/:id" => "users#update" 
    delete "/cities/:city_id/school/:school_id/user/:id" => "users#destroy" 

秀學校

<% title "#{@school.name}" %> 

<div class="row"> 
    <div class="col-md-12 text-center city-title"> 
     <h3><%= @school.name %></h3> 
    </div> 
</div> 

<%= search_form_for @search do |f| %> 
    <%= f.label :full_name_cont %> 
    <%= f.text_field :full_name_cont %> 

    <%= f.submit %> 
<% end %> 

<div class="users"> 
    <% @users.in_groups_of(4, false).each do |users| %> 

     <div class="row"> 

      <% users.each do |user| %> 
       <div class="col-xs-6 col-md-3 text-center"> 
        <a href="<%= user_path(@city, @school, user) %>" class="thumbnail school"> 
         <%= image_tag user.avatar.url(:thumb), size: "100", class: "user-profile-image img-responsive" %> 
         <%= user.full_name %> 
        </a> 
       </div> 
      <% end %> 

     </div> 

    <% end %> 

    <div class="row"> 
     <div class="col-md-12"> 
      <%= link_to city_path(@city), class: "btn btn-primary" do %> 
       <span class="glyphicon glyphicon-chevron-left"></span> Back 
      <% end %> 

      <% if is_admin? %> 
       <%= link_to new_user_path(@city, @school), class: "btn btn-primary" do %> 
        <span class="glyphicon glyphicon-plus"></span> Add user 
       <% end %> 
      <% end %> 
     </div> 
    </div> 

</div> 

觀點我得到的錯誤:

No route matches {:action=>"index", :city_id=>"1", :controller=>"users", :format=>nil, :id=>"3"} missing required keys: [:school_id] 

我怎麼能做到這一點?

+0

我們展示您的視圖代碼太 – MurifoX

+0

@MurifoX它現在增加。 – Nikola

回答

0

問題解決了;我不得不用洗劫對象一起添加URL:

<%= search_form_for @search, url: school_path(@city, @school) do |f| %>