我如何显示前一篇文章是使用simple_form gem创建的 - Ruby-on-Rails

时间:2015-02-06 12:34:01

标签: ruby-on-rails

我希望能够在我的Rails网站上显示帖子的发布日期,我将如何在1天前显示发帖'等等,不到一天前'对于任何一天的事情。

我使用的是simple_form gem,对Rails来说很新,所以不知道该怎么做。任何对此的帮助都会非常感激!

我的代码如下:

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

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你可以使用rails-timego gem,表格与它无关:

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

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

如果您设置正确,您将通过javascript自动更新时间。一分钟前,2分钟前,等等。最多1天,然后它将是1天前,2天前等。