ruby on rails - RoR: Selecting Certain Posts Based on a Column Value -
i have been trying teach myself ror. have been working through tutorials on past few weeks, trying add of own functionality app go. feature trying implement seems quite simple, doubt solution safe , elegant be.
so far, users can submit post site. model consists of image, title, category, , description. users can comment on given post (the comments contained in table). trying add dropdown menu on navigation bar allows users browse specific categories. in other words, index should display posts within category user has selected. while solution works, unhappy it. here code navigation bar:
%button#menu1.btn.btn-primary.dropdown-toggle{"data-toggle" => "dropdown", :type => "button"} explore %span.caret %ul.dropdown-menu{"aria-labelledby" => "menu1", :role => "menu"} %li{:role => "presentation"} %a = link_to "all", posts_path(categories: "all") %li{:role => "presentation"} %a = link_to "3d modeling", posts_path(categories: "3d modeling") %li{:role => "presentation"} %a = link_to "design", posts_path(categories: "design") %li{:role => "presentation"} %a = link_to "drawing", posts_path(categories: "drawing") %li{:role => "presentation"} %a = link_to "photography", posts_path(categories: "photography") %li{:role => "presentation"} %a = link_to "printmaking", posts_path(categories: "printmaking") %li{:role => "presentation"} %a = link_to "sculpture", posts_path(categories: "sculpture") %li{:role => "presentation"} %a = link_to "other", posts_path(categories: "other")
here code index method in posts controller:
if params[:categories] && params[:categories] == "all" @posts = post.all.order('created_at desc').page params[:page] elsif params[:categories] && params[:categories] == "3d modeling" @posts = post.where(category: "3d modeling").page params[:page] elsif params[:categories] && params[:categories] == "design" @posts = post.where(category: "design").page params[:page] elsif params[:categories] && params[:categories] == "drawing" @posts = post.where(category: "drawing").page params[:page] elsif params[:categories] && params[:categories] == "photography" @posts = post.where(category: "photography").page params[:page] elsif params[:categories] && params[:categories] == "printmaking" @posts = post.where(category: "printmaking").page params[:page] elsif params[:categories] && params[:categories] == "sculpture" @posts = post.where(category: "sculpture").page params[:page] elsif params[:categories] && params[:categories] == "other" @posts = post.where(category: "other").page params[:page] end
finally, here code in routes file:
resources :post resources :comments end root 'posts#index'
it seems though should handling routes differently? or not case since category column part of post model , not own table? help!
try this
if params[:categories].present? @posts = post.where(category: params[:categories]).page(params[:page]) else @posts = post.all.order('created_at desc').page(params[:page]) end
and remove category of link_to category: "all"
<%= link_to "all", posts_path %>
Comments
Post a Comment