所以我正在使用云数据存储和ruby on rails上的社交网络应用程序。我已经完成了应用程序发布方面的crud操作,并且我试图为我的评论方面进行操作。当我尝试在单个帖子页面中呈现我的评论表单时,我遇到了错误。我收到一个错误,我认为它与我的一个控制器或我的erb文件有关。我现在已经看了很久了,所以希望有人可以对此有所了解。我尝试了这个解决方案,但它没有用。 link
作为参考,我基本上遵循Google Datastore Bookshelf教程应用程序。 BookShelf Tutorial App
这是我的帖子控制器
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
flash[:success] = "Updated Book"
redirect_to posts_path(@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
这是我的评论控制器
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
评论模型
require "google/cloud/datastore"
class Comment
include ActiveModel::Model
attr_accessor :id, :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
def to_entity
post.parent = dataset.key "Post"
entity = Google::Cloud::Datastore::Entity.new
entity.key = post
entity["body"] = body
entity
end
# [START from_entity]
def self.from_entity entity
comment = Comment.new
comment.id = entity.key.id
entity.properties.to_hash.each do |name, value|
comment.send "#{name}=", value if comment.respond_to? "#{name}="
end
comment
end
# [END from_entity]
# Save the book to Datastore.
# @return true if valid and saved successfully, otherwise false.
def save
if valid?
entity = to_entity
Book.dataset.save entity
self.id = entity.key.id
true
else
false
end
end
def persisted?
id.present?
end
发布模型
require "google/cloud/datastore"
class Post
include ActiveModel::Model
attr_accessor :id, :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
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
# [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 Post by ID. Returns Post 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]
# [START update]
# Set attribute values from provided Hash and save to Datastore.
def update attributes
attributes.each do |name, value|
send "#{name}=", value if respond_to? "#{name}="
end
save
end
# [END update]
# [START destroy]
def destroy
Post.dataset.delete Google::Cloud::Datastore::Key.new "Post", id
end
# [END destroy]
# 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
def persisted?
id.present?
end
end
我的show.html.erb文件
<h1><%= @post.title %></h1>
<p><%= @post.body %></p>
<hr>
<%= link_to 'Edit', edit_post_path(@post), :class => 'btn btn-default' %>
<%= link_to 'Delete', post_path(@post),
method: :delete,
data: {confirm: 'Are You Sure?'},
:class => 'btn btn-danger' %>
<%= render 'comments/form'%>
这是评论html erb文件
<h3>Comments</h3>
<%= @post.comment.each do |comment| %>
<div class="well">
</p><%= link_to '[X]', [comment.post, comment],
method: :delete,
data: {confirm: 'Are You Sure?'} %></p>
</div>
<% end %>
这是错误日志
ActionView::Template::Error (undefined method `comment' for #<Post:0xbb05498>):
1: <h3>Add Comment</h3>
2: <%= form_for([@post, @post.comment.build]) do |f| %>
3: <div class="form-group">
4: <%= f.label:body %><br>
5: <%= f.text_area(:body, {:class => 'form-control'}) %>
app/views/comments/_form.html.erb:2:in `_app_views_comments__form_html_erb__676677109_98008740'
app/views/posts/show.html.erb:12:in `_app_views_posts_show_html_erb___922952118_98050956'