ruby on rails - How do I sort posts by category per user? -
i'm new ruby on rails , i've been working on app collectors of funko pops can login , log funko pops have. it's simple, , there few fields entering information funkos themselves; title, description , category along ability upload image of funko users home screen displays shelf, funkos on.
anyway. have functionality built in, allowing users sort home screen category, if user clicks dropdown , selects "marvel" should see marvel funko pops have. if have no funkos category of "marvel" message appears saying there no funkos in category. functionality works perfectly, when create new user , sign in, , sort category, shows of funkos in selected category but users.
i want able display funkos category, on per user basis, funkos user has entered should sortable using categories dropdown.
here code main controller:
class funkoscontroller < applicationcontroller before_action :find_funko, only:[:show, :edit, :update, :destroy] def index if user_signed_in? && params[:category].blank? @funkos = funko.where(:user_id => current_user.id).all.order("created_at desc") elsif params[:category].present? @category_id = category.find_by(name: params[:category]).id @funkos = funko.where(:category_id => @category_id).order("created_at desc") else end end def show end def new @funko = current_user.funko.build @categories = category.all.map{ |c| [c.name, c.id]} end def create @funko = current_user.funko.build(funko_params) @funko.category_id = params[:category_id] if @funko.save redirect_to root_path else render 'new' end end def edit @categories = category.all.map{ |c| [c.name, c.id]} end def update @funko.category_id = params[:category_id] if @funko.update(funko_params) redirect_to funko_path(@funko) else render 'edit' end end def destroy @funko.destroy redirect_to root_path end private def funko_params params.require(:funko).permit(:title, :description, :category_id, :funko_img) end def find_funko @funko = funko.find(params[:id]) end end
hopefully first post/question on here clear , concise enough answer i'm looking for!
thanks
tom
however when create new user , sign in, , sort category, shows of funkos in selected category users.
the problem lies here
@funkos = funko.where(:category_id => @category_id).order("created_at desc")
you not fetching funkos
per user if params[:category]
present. add that, , go
@funkos = funko.where(:category_id => @category_id, :user_id => current_user.id).order("created_at desc")
Comments
Post a Comment