Joel
Joel

Reputation: 97

Couldn't find [model] with 'id'=create

My app works fine by calling a service in the create action of the controller. After creation, the record shows up using the index action. When I add in the show action and attempt to create a new record using the service call, it checks the show action and tells me "Couldn't find Document with 'id'=create"

There is no save within the create action but rather in the service called by the create action. What is happening to where the show action check is resulting in this error?

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

Upvotes: 0

Views: 311

Answers (2)

Joel
Joel

Reputation: 97

This was resolved by explicitly stating my routes for the documents controller as below.

get "documents/query"

get "documents/index"

get "documents/create"

get "documents/show"

Upvotes: 1

ddgd
ddgd

Reputation: 1717

You have a routing problem. And the reason is that you're using singular object name in the controller name, and rails gets confused. Change your controller name to AlchemiesController

And then to create a new record do a POST request to the alchemies_path, e.g. example.com/alchemies.

And to show a record, use alchemy_path(@alchemy)

Upvotes: 0

Related Questions