所以我正在使用rails和google云数据存储的web应用程序。我在编写网络应用程序方面相对较新。我遇到的大多数错误都来自于尝试将数据存储与rails MVC模型集成。对于更多上下文,我的大部分代码都基于数据存储信息页面上的Google Datastore Bookshelf教程。 https://cloud.google.com/ruby/getting-started/using-cloud-datastore
最近,当我尝试将帖子保存到数据存储区数据库时,我遇到了PostsController #creore中的NameError。我似乎无法找到这个问题的原因所以希望有人可以成为我新的眼睛或提供一些见解。
此外,如果您知道我可以使用数据存储区和rails找到有用文档的任何地方。那将是值得赞赏的
所以这是我的post_controller.rb文件
class PostsController < ApplicationController
PER_PAGE = 10
def index
@post, @cursor = Post.query limit: PER_PAGE, cursor: params[:cursor]
end
def new
@post = Post.new
end
def create
@post = Post.new post_params
if @post.save
flash[:success] = "Posted"
redirect_to posts_path(@post)
else
render :new
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def show
@post = Post.find(params[:id])
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
这是post post_rb的模型
require "google/cloud/datastore"
class Post
include ActiveModel::Model
attr_accessor :title, :body
# Return a Google::Cloud::Datastore::Dataset for the configured dataset.
# The dataset is used to create, read, update, and delete entity objects.
def self.dataset
@dataset ||= Google::Cloud::Datastore.new(
project: Rails.application.config.
database_configuration[Rails.env]["dataset_id"]
)
end
# Query Book entities from Cloud Datastore.
#
# returns an array of Book query results and a cursor
# that can be used to query for additional results.
def self.query options = {}
query = Google::Cloud::Datastore::Query.new
query.kind "Post"
query.limit options[:limit] if options[:limit]
query.cursor options[:cursor] if options[:cursor]
results = dataset.run query
posts = results.map {|entity| Post.from_entity entity }
if options[:limit] && results.size == options[:limit]
next_cursor = results.cursor
end
return posts, next_cursor
end
# [START from_entity]
def self.from_entity entity
post = Post.new
post.id = entity.key.id
entity.properties.to_hash.each do |name, value|
post.send "#{name}=", value if post.respond_to? "#{name}="
end
post
end
# [END from_entity]
# [START find]
# Lookup Book by ID. Returns Book or nil.
def self.find id
query = Google::Cloud::Datastore::Key.new "Post", id.to_i
entities = dataset.lookup query
from_entity entities.first if entities.any?
end
# [END find]
# [START validations]
# Add Active Model validation support to Book class.
include ActiveModel::Validations
validates :title, presence: true
# [END validations]
# Save the book to Datastore.
# @return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Post.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
end
这是我用来调用数据存储区中的save方法的new.html.erb文件
<h1>Create Post</h1>
<%= form_for @post do |f| %>
<% if @post.errors.any? %>
<% @post.errors.full_messages.each do |msg| %>
<div class="alert alert danger"><%= msg %></div>
<% end %>
<% end %>
<div class="form-group">
<%= f.label:title %><br>
<%= f.text_field(:title, {:class => 'form-control'}) %>
</div>
<div class="form-group">
<%= f.label:body %><br>
<%= f.text_area(:body, {:class => 'form-control'}) %>
</div>
<p>
<%= f.submit({:class => 'btn btn-primary'}) %>
</p>
<% end %>
这是我的routes.rb文件
Rails.application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
resources :sessions, only: [:create, :destroy]
resource :main, only: [:show]
resources :posts
root to: 'posts#index', as: "home"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
编辑:忘记包含错误消息日志
NameError (undefined local variable or method `to_entity' for #<Post:0x65b0d70>
Did you mean? to_key):
app/models/post.rb:72:in `save'
app/controllers/posts_controller.rb:16:in `create'
注意:create.html.erb为空,因此我选择不包含它
答案 0 :(得分:1)
好的,看起来你错过了to_entity
方法。尝试为您的帖子模型添加类似的内容。我稍微修改了Book
文档示例here中的代码。您将需要阅读整个文件,以查看所有代码的Book模型。
def to_entity
entity = Google::Cloud::Datastore::Entity.new
entity.key = Google::Cloud::Datastore::Key.new "Post", id
entity["title"] = title
entity["body"] = body
entity
end