简体   繁体   中英

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.

I'm using the simple_form gem and am very new to Rails so not sure what to do. Any help with this would be really appreciated!

My code is as follows:

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 -

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 -

<% @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 -

<%= 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:

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. a minute ago, 2 minutes ago, etc.. up to 1 day, then it will be 1 day ago, 2 days ago, etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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