alex lug alex lug - 4 months ago 10
Ruby Question

routes issues in layout

Ok so i'm getting this error -
No route matches {:action=>"search", :controller=>"drawings", :search=>"2016"} missing required keys: [:workcategory_id]

I have this in my layouts/_header

<% unless @workcategories.nil? %>
<% @workcategories.each do |workcategory| %>
<li><%= link_to workcategory.name, search_workcategory_drawings_path(search: workcategory.name) %></li>
<% end %>


my drawing model

belongs_to :workcategory
def self.search(params)
drawings = Drawing.where("name like ?", "%#{params[:search]}%")
drawings
end


and this is the part of the controller where things should be happening

def search
@drawings = Drawing.search(params)
end


lastly my routes

resources :workcategories do
resources :drawings do
collection do
get :search
end
end
end


So the issue is, that when i go on any page - home, admin panel etc .. i get that error 'No route matches {:action=>"search", :controller=>"drawings", :search=>"2016"} missing required keys: [:workcategory_id]'. I don't understand why it's trying to 'build' the search path already, although i'm not trying to access it. I hope i'm making sense ...

Answer

Simply provide a workcategory_id :

      <li><%= link_to workcategory.name, search_workcategory_drawings_path(workcategory.id, search: workcategory.name) %></li>