找到你要的答案

Q:How to show posts onto a static page

Q:如何在静态页面上显示帖子

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?

我创建了一个图片上传型网站。我可以登录和上传图像,它显示完美。

但是我想为没有登录的用户创建一个登陆页面,并且可以在登陆页面上看到一些图片。

每当有人上传的图像,它应该更新登陆页以及。有点像降序。

我试图创造的顶部视图/布局/ application.html.haml产量静态页面,但它没有工作,因为每当我上传的东西,它会给我的登陆页面,然后在底部,形成。

谁能告诉我如何做到这一点?

answer1: 回答1:

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

假设你的根路径指向你的登陆页面,

root 'welcome#index'

在控制器中,获取要在登陆页上显示的图像记录,

class WelcomeController < ApplicationController

  def index
    @images = Image.last(10)
  end

end

在视图中使用它们,

- @images.each do |image|
  # put them in img tag
ruby-on-rails  static  posts