帖子中的RoR NoMethodError#new

时间:2017-07-19 09:15:28

标签: ruby-on-rails ruby google-cloud-datastore

所以我一直在使用rails和google数据存储区构建应用。我在new.html.erb文件中遇到错误,我收到了NoMethodError。

我一直在寻找解决问题的方法,而且很可能是我代码中的一个简单错字。我已经盯着代码几个小时,所以也许一些新鲜的眼睛可以帮助我弄明白

这是我的posts_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

这是我的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 %>

这是我的模型post.rb

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

的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

编辑:这是我收到的错误日志

 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'

2 个答案:

答案 0 :(得分:1)

您已在@posts方法中定义了new,但您在@post中使用new.html.erb。这是错误的原因。保持同名@posts@post

答案 1 :(得分:1)

您的Post模型是一个普通的ruby对象,但您将其视为ActiveModel / ActiveRecord对象。

尝试添加

include ActiveModel::Model模型中的

Post如下:

class Post
  include ActiveModel::Model
  ...
end
相关问题