通过在控制器的创建操作中调用服务,我的应用程序正常工作。创建后,记录显示使用索引操作。当我添加show动作并尝试使用服务调用创建新记录时,它会检查show动作并告诉我“无法找到带有'id'的文档=创建”
在create操作中没有保存,而是在create action调用的服务中。 show动作检查导致此错误的位置发生了什么?
require 'alchemyapi_ruby/alchemyapi'
require 'json'
class DocumentsController < ApplicationController
before_action :authenticate_user!
def index
@documents = Document.all
@concepts = Concept.all
@entities = Entity.all
@keywords = Keyword.all
@relations = Relation.all
@relation_objects = RelationObject.all
@sentiments = Sentiment.all
end
def show
@document = Document.find(params[:id])
end
def create
alchemy_api_parser = AlchemyapiParser.new(params, current_user)
alchemy_api_parser.call
if alchemy_api_parser.successful?
flash[:notice] = "Input was successfully analyzed and persisted."
redirect_to alchemy_index_path
else
flash[:error] = "There was a problem analyzing your input. Please try again."
redirect_to alchemy_index_path
end
end
end
#routes.rb
Rails.application.routes.draw do
devise_for :users
resources :welcome
resources :documents
resources :users, only: [:update]
get 'about' => 'welcome#about'
get 'Contact Us' => 'welcome#contact'
get 'search' => 'documents#search'
root to: 'welcome#index'
end
答案 0 :(得分:0)
您遇到路由问题。原因是你在控制器名称中使用单个对象名称,并且rails会混淆。将您的控制器名称更改为AlchemiesController
然后创建新记录,向alchemies_path
发出POST请求,例如example.com/alchemies
。
要显示记录,请使用alchemy_path(@alchemy)
答案 1 :(得分:0)
通过明确说明文档控制器的路由,解决了这个问题。
get "documents/query"
get "documents/index"
get "documents/create"
get "documents/show"