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:

  1. routes.rb

    resources :workcategories   :drawings, to: 'drawings#workcategory_drawings' end resources :drawings 

this gives you:

/workcategories/:workcategory_id/drawings => drawings#workcategory_drawings

  1. drawings_controller.rb

    .. def workcategory_drawings   @drawings = drawing.where(workcategory_id: params[:workcategory_id]).all end .. 

(no need before_action :list)

  1. the dropdown (assuming have initialized @workcategories somewhere)

    <% @workcategories.each |workcategory| %>   <li>     <%= link_to workcategory.name, workcategory_drawings_path(workcategory) %>   </li> <% end %> 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -