ruby - rails routing list filter from html -
so want dropdown, able select or filter, drawings based on workcategory at. both models (drawings , workcategories) connected has_many drawings , belongs_to workcategories. drawing holds workcategory.id foreign key, , want filter it.this i've tried build
<% @workcategories.each |workcategory| %> <li><%= link_to workcategory.name, workcategory_list_path(workcategory.id) %></li> <% end %>
while in drawings controller i've added these :
before_filter :list def list @drawingsz = drawing.order("drawings.id asc").where(:workcategory_id => @workcategory.try(:id)) end
the problem is, whenever press 1 of workcategories inserted, shows me drawings. i've tried basic variation others, including routings. oh , talk routings, have :
resources :workcategories :list, :controller => :drawings resources :drawings end resources :drawings
any ideas ? i've browsed forums lot last night, , routes pages on official docus, can't myself around it. pages : /workcategory.name/drawings. thank !!
update - added controller
class drawingscontroller < applicationcontroller before_action :find_drawing, only: [:edit, :update, :destroy] before_filter :list def index @drawings = drawing.all.order("created_at desc") end def new @drawing = drawing.new end def create @drawing = drawing.new(drawing_params) if @drawing.save flash[:notice] = "drawing created" redirect_to(:action=>'index', :drawing_id => @drawing.id) else @drawings = drawings.order() render('new') end end def edit end def update if @drawing.update_attributes(drawing_params) flash[:notice] = "drawing updated." redirect_to(:action=>"index") else render("edit") end end def destroy @drawing.destroy redirect_to drawings_path end def list @drawingsz = drawing.order("drawings.id asc").where(:workcategory_id => @workcategory.try(:name)) end private def find_drawing @drawing=drawing.find(params[:id]) end def drawing_params params.require(:drawing).permit(:name, :description, :image) end end
view update new view update - doesn't work, due error.
<% @workcategories.each |workcategory| %> <li><%= link_to workcategory.name, {:controller => 'drawings', :action => 'list', :workcategory_id => workcategory.id } %></li> <% end %>
i've updated list :
def list @workcategory_id = params[:workcategory_id] if @workcategory_id @drawingz = drawing.find(params[:workcategory_id]).workcategory_id end end
now error
couldn't find drawing 'id'=2015
which odd, because 2015 in names, not in ids. feel i'm closer still missing ....
here cleaned version of trying do:
routes.rb
resources :workcategories :drawings, to: 'drawings#workcategory_drawings' end resources :drawings
this gives you:
/workcategories/:workcategory_id/drawings => drawings#workcategory_drawings
drawings_controller.rb
.. def workcategory_drawings @drawings = drawing.where(workcategory_id: params[:workcategory_id]).all end ..
(no need before_action :list
)
the dropdown (assuming have initialized
@workcategories
somewhere)<% @workcategories.each |workcategory| %> <li> <%= link_to workcategory.name, workcategory_drawings_path(workcategory) %> </li> <% end %>
Comments
Post a Comment