食事の写真を投稿するサイトを作っています。
そこで投稿した写真を一覧表示できるようにしています。
投稿時の特定の文字列を抜き取り種類別に表示させる色やsizeを変えています。
order(created_at:"desc")で新しい順に変えていますが、特定の文字列により種類別に表示させる場所を変えているので同じ種類の表示box内では順番は変わります。
全ての写真を投稿の新しい順に並び替えたいですがやり方がわかりません。
ご教授お願いします。
ファイル名posts_controller.rb class PostsController < ApplicationController def index @breakfast = Post.all.order(created_at:"desc").where(meals: "Breakfast") @lunch = Post.all.order(created_at:"desc").where(meals: "Lunch") @afternoonsnack = Post.all.order(created_at:"desc").where(meals: "Afternoon Snack") @dinner = Post.all.order(created_at:"desc").where(meals: "Dinner") @restaurant = Post.all.order(created_at:"desc").where(meals: "Favorite Restaurant") end def new @post = Post.new end def create @post = Post.new(post_params) if @post.save redirect_to root_path, notice: "出品完了やで!!!" else render :new, alert: '出品できませんでした' end end private def post_params params.require(:post).permit(:image, :meals, :description).merge(user_id: current_user.id) end end
ファイル名index.html.haml %section.item.item-l.item-breakfast %a{href: "#"} = image_tag "image_L_1.jpg",class:"image-L" .category Breakfast %p.description テキストテキストダミーテキストダミーテキスト - @breakfast.each do |breakfast| %section.item.item-breakfast %a{href: "#"} = image_tag "#{breakfast.image}", class:"image" .category Breakfast %p.description = breakfast.description - @lunch.each do |lunch| %section.item.item-lunch %a{href: "#"} = image_tag "#{lunch.image}", class:"image" .category Lunch %p.description = lunch.description - @afternoonsnack.each do |afternoonsnack| %section.item.item-m.item-afternoon-snack %a{href: "#"} = image_tag "#{afternoonsnack.image}",class:"image",style:"height:auto;" .category Afternoon Snack %p.description = afternoonsnack.description - @dinner.each do |dinner| %section.item.item-dinner %a{href: "#"} = image_tag "#{dinner.image}", class:"image" .category Dinner %p.description = dinner.description - @restaurant.each do |restaurant| %section.item.item-m.item-lunch %a{href: "#"} = image_tag "#{restaurant.image}",class:"image",style:"height:auto;" .category Favorite Restaurant %p.description = restaurant.description %script{src:"http://cdnjs.cloudflare.com/ajax/libs/masonry/3.3.0/masonry.pkgd.js"} :javascript window.onload = function(){ new Masonry('body',{ itemSelector: '.item', columnWidth: 180, gutter: 4 }); };
回答1件
あなたの回答
tips
プレビュー