Ruby on rails RoR NoMethodError在Postsnew中
因此,我一直在使用rails和google数据存储构建一个应用程序。我在新的.html.erb文件中不断遇到一个错误,在那里我得到了NoMethodError 我一直在寻找我的问题的解决方案,很可能是代码中的某个简单输入错误。我已经盯着代码看了好几个小时了,也许一些新的眼睛可以帮我弄明白 这是我的posts_controller.rb routes.rb 编辑:这是我得到的错误日志 您已经在新方法中定义了@posts,但在new.html.erb中使用了@post。这就是错误的原因。保持相同的名称@posts或@post您的post模型是一个普通的ruby对象,但您将其视为ActiveModel/ActiveRecord对象 尝试添加 在Post模型中包括ActiveModel::Model,如下所示:Ruby on rails RoR NoMethodError在Postsnew中,ruby-on-rails,ruby,google-cloud-datastore,Ruby On Rails,Ruby,Google Cloud Datastore,因此,我一直在使用rails和google数据存储构建一个应用程序。我在新的.html.erb文件中不断遇到一个错误,在那里我得到了NoMethodError 我一直在寻找我的问题的解决方案,很可能是代码中的某个简单输入错误。我已经盯着代码看了好几个小时了,也许一些新的眼睛可以帮我弄明白 这是我的posts_controller.rb routes.rb 编辑:这是我得到的错误日志 您已经在新方法中定义了@posts,但在new.html.erb中使用了@post。这就是错误的原因。保持相同的名
class Post
include ActiveModel::Model
...
end
我想你的意思是把索引方法中的帖子改成帖子。控制器和新的.html.erb文件中的post相同。我还是一样error@KaodilichiNwuda你能用完整的错误信息更新这个问题吗?@kaodilichinwoda你在新方法中有@posts。将其更改为@post并再次检查,以免我忘记更新控制器代码。我还是一样error@KaodilichiNwuda尝试重新启动服务器。请发布错误日志。此处有许多不相关的代码。我建议你试着找一个更简单的例子,它会产生同样的错误。@kaodilichinwoda如果答案解决了你的问题,请将它标记为你问题的公认答案。
<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 %>
class Post
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| Book.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
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
ActionView::Template::Error (undefined method `to_key' for #<Post:0x2ae2c68>
Did you mean? to_query):
1: <h1>Create Post</h1>
2: <%= form_for @post do |f| %>
3: <% if @post.errors.any? %>
4: <% @post.errors.full_messages.each do |msg| %>
5: <div class="alert alert danger"><%= msg %></div>
app/views/posts/new.html.erb:2:in `_app_views_posts_new_html_erb__241418705_22466964'
class Post
include ActiveModel::Model
...
end