简体   繁体   English

我如何显示前段时间使用simple_form宝石发表的帖子-Ruby-on-Rails

[英]How would I display the time ago that a post was made with the simple_form gem - Ruby-on-Rails

I want to be able to display the date that a post was made in my Rails site, how would I display say '1 day ago' etc and 'less than a day ago' for anything under a day. 我希望能够显示在我的Rails网站上发布帖子的日期,如何显示一天之内的任何内容,例如“ 1天前”等和“小于一天前”。

I'm using the simple_form gem and am very new to Rails so not sure what to do. 我正在使用simple_form gem,并且对Rails来说是个新手,所以不确定该怎么做。 Any help with this would be really appreciated! 任何帮助,将不胜感激!

My code is as follows: 我的代码如下:

house_controller.rb - house_controller.rb-

class HousesController < ApplicationController
  def index
    @houses = House.page(params[:page]).per(20).order(created_at: :desc)
  end

  def new
    @house = House.new
  end

  def create
    @house = House.new(params.require(:house).permit(:title, :price, :location, :beds, :property, :available, :description, :image))
      if @house.save
        redirect_to root_path
      else
        render "new"
      end
  end
end

house.rb - house.rb-

class House < ActiveRecord::Base
    validates :title, presence: true
    validates :price, presence: true
    validates :location, presence: true
    validates :property, presence: true
    validates :beds, presence: true
    validates :available, presence: true
    validates :description, presence: true
    validates :image, presence: true

    has_attached_file :image, styles: { large: "600x600", medium: "300x300", thumb: "150x150#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

index.html.erb - index.html.erb-

<% @houses.each do |house| %>

  <div class="house">
    <%= image_tag house.image.url(:large) %>
    <h2><%= house.title %></h2>
    <h2>&#163;<%= house.price %></h2>
    <p><%= house.location %></p>
    <p><%= house.beds %></p>
    <p><%= house.available %></p>
    <p><%= house.property %></p>
    <p><%= house.description %></p>
  </div>
<% end %>

new.html.erb - new.html.erb-

<%= simple_form_for @house, html: { multipart: true } do |form| %>
  <%= form.input :title, label: "House title" %>
  <%= form.input :price, label: "House price" %>
  <%= form.input :location, label: "Town, County" %>
  <%= form.input :beds, label: "No. of Bedrooms" %>
  <%= form.input :available, label: "House available from" %>
  <%= form.input :property, label: "Property Type e.g Flat, Bungalow" %>
  <%= form.input :description, label: "Describe your house" %>
  <%= form.input :image, as: :file, label: "Image of house" %>
  <%= form.button :submit %>
<% end %>

If I understood correctly your question, you can use rails-timego gem, form has nothing to do with it: 如果我正确理解了您的问题,则可以使用rails-timego gem,与表单无关:

https://github.com/jgraichen/rails-timeago https://github.com/jgraichen/rails-timeago

<%= timeago_tag @house.created_at, :nojs => true, :limit => 1.day.ago %>

if you'll set up correctly you'll have the time updated automatically by javascript. 如果设置正确,您将通过javascript自动更新时间。 a minute ago, 2 minutes ago, etc.. up to 1 day, then it will be 1 day ago, 2 days ago, etc.. 一分钟前,2分钟前等。直到1天,然后是1天前,2天前等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM