[英]ActiveRecord creating new objects every time I load the index.erb
I'm working on creating a simple wiki website using Sinatra and ActiveRecord. 我正在使用Sinatra和ActiveRecord创建一个简单的Wiki网站。 I'm having a problem where every time I try to load the index view for versions of a specific document, A new version is being created.
我遇到一个问题,每次尝试加载特定文档版本的索引视图时,都会创建一个新版本。 I've taken a look at my routes and my Version model, but just can't seem to figure it out, although it is probably something glaring (I'm a beginner).
我看了一下我的路线和我的Version模型,但似乎无法弄清楚,尽管它可能很刺眼(我是初学者)。 Any help would be appreciated :) Some of the relevant code:
任何帮助将不胜感激:)一些相关代码:
VERSIONS_CONTROLLER.rb VERSIONS_CONTROLLER.rb
# ===============
# Versions
# ===============
# INDEX
get '/documents/:document_id/versions' do
@document = Document.find(params[:document_id])
@versions = Version.where(["document_id = ?", params[:document_id]])
erb :'versions/index'
end
# NEW
get '/documents/:id/versions/new' do
@authors = Author.all()
@document = Document.find(params[:id])
erb :'versions/new'
end
# SHOW
get '/documents/:document_id/versions/:id' do
# @versions = Version.find(params[:document_id])
@document=Document.find(params[:document_id])
@version = Version.find(params[:id])
erb :'versions/show'
end
# CREATE
post '/documents/:document_id/versions' do
version = Version.new(params[:version])
document.update(body: version.content)
version.save
redirect("/documents/#{ params[:document_id] }")
end
# REVERT
# The revert function is going to post to
# the create function above. It is going to
# do this via a revert button under each
# button on the versions/index.erb page.
# When pressed, the button will post a
# /documents/:document_id/versions form
# with the params[:document_id][:version]
# set to equal those of the version under
# which the button is located
VERSIONS/INDEX.ERB 版本/索引
<div class="versions-index-wrapper">
<div class="versions-index-header">
<h1>
Version history for document: <%= @document.title %>
</h1>
</div>
<div class="versions-index-list">
<% @versions.each do |version| %>
<li>
<a href="/documents/<%= @document.id %>/versions/<%= version.id %>">
COMMIT: <%= version.blurb %><hr>
CREATED AT:<%= version.created_at %><hr> BY:
<%= version.author.username %><hr>
</a>
</li>
<% end %>
</div>
</div>.
VERSIONS MODEL 版本模型
class Version < ActiveRecord::Base
belongs_to :document
belongs_to :author
has_many :comments, dependent: :destroy
def self.latest
self.order("created_at DESC")
end
end
SCHEMA 施玛
DROP TABLE IF EXISTS comments CASCADE;
DROP TABLE IF EXISTS versions CASCADE;
DROP TABLE IF EXISTS documents CASCADE;
DROP TABLE IF EXISTS authors;
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255),
username VARCHAR(255),
points INTEGER,
icon_url VARCHAR(255),
github_url VARCHAR(255),
twitter_url VARCHAR(255)
);
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
title VARCHAR(255),
img_url VARCHAR(255),
created_at TIMESTAMP,
updated_at TIMESTAMP,
author_id INTEGER,
body TEXT
);
CREATE TABLE versions (
id SERIAL PRIMARY KEY,
blurb TEXT,
content TEXT,
created_at TIMESTAMP,
document_id INTEGER references documents,
author_id INTEGER references authors
);
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP,
version_id INTEGER references versions,
author_id INTEGER references authors,
content VARCHAR(512)
);
DOCUMENTS.rb DOCUMENTS.rb
class Document < ActiveRecord::Base
has_many :versions, dependent: :destroy
has_many :authors, through: :versions
after_initialize :init
def init
last_updated = Time.now.utc.iso8601.gsub('-', '').gsub(':', '')
self.versions << Version.create({
content: "version 1.0",
blurb: "v1.0",
author_id: Author.first.id
})
end
def self.latest
self.order("updated_at DESC")
end
def self.alphabetical
self.order("title ASC")
end
end
The after_initialize
callback runs every time an ActiveRecord object is loaded into Rails. 每当将ActiveRecord对象加载到Rails中时 ,
after_initialize
回调就会运行。 Since your app is loading the document on every request, your init
method is being called every time, generating a new version. 由于您的应用程序是根据每个请求加载文档的,因此每次都会调用
init
方法,从而生成新版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.