Search code examples
ruby-on-railsstaticposts

How to show posts onto a static page


I have created a image uploading type website. I can login and upload images and it shows perfectly.

But I want to create a landing page for users who are not logged in and can see few images on the landing page.

And whenever someone uploads an image, it should update on the landing page as well. Kinda like in descending order.

I tried to create a static page on top of the yield in the views/layouts/application.html.haml but it didn't work because whenever I'll upload something, it will show me the landing page then on the bottom, the form.

Can anyone tell me how to accomplish this?


Solution

  • Assuming your root route points to your landing page,

    root 'welcome#index'
    

    In controller, fetch the image records you want to show on the landing page,

    class WelcomeController < ApplicationController
    
      def index
        @images = Image.last(10)
      end
    
    end
    

    Use them in views,

    - @images.each do |image|
      # put them in img tag