Filter results on index page from dropdown

前端 未结 1 1270
星月不相逢
星月不相逢 2021-02-04 18:45

I\'m using rails 3 and I have two models, venues and areas where each area has many venues and each venue belongs to one area.

I\'m trying to find a way of filtering the

1条回答
  •  清酒与你
    2021-02-04 19:30

    You can find the Venues in your controller depending on whether an area is selected or not. You can modify the view to send the area name, instead of the area id(which might make it easier):

    
    <%= select("area", "name", Area.all.collect(&:name)) %>
    
    

    The controller would look something like this -

    
    def index
      if (params[:area] && Area.all.collect(&:name).include?(params[:area][:name]))
         @venues = Venue.send(params[:area][:name].downcase)
      else
         @venues = Venue.all
      end
    end
    
    

    0 讨论(0)
提交回复
热议问题