我使用Rails Api。在“属性”表中,“属性类型”字段使用数字。我想用某些单词代替这些数字。为此,我执行以下操作:
attributes_controller.rb:
class AttributesController < ApplicationController
before_action :set_attribute, only: [:show, :update, :destroy]
def index
@attributes = Attribute.all
render json: @attributes
end
def show
render json: @attribute
end
def create
@attribute = Attribute.new(attribute_params)
if @attribute.save
render json: @attribute, status: :created, location: @attribute
else
render json: @attribute.errors, status: :unprocessable_entity
end
end
def update
if @attribute.update(attribute_params)
render json: @_attribute
else
render json: @attribute.errors, status: :unprocessable_entity
end
end
def destroy
@attribute.destroy
end
private
def set_attribute
@attribute = Attribute.find(params[:id])
end
def attribute_params
params.require(:attribute).permit!
end
end
attribute.rb:
class Attribute < ActiveRecord::Base
extend ActiveHash::Associations::ActiveRecordExtensions
self.table_name = 'ATTRIBUTS'
self.primary_key = 'attribut_id'
belongs_to :attribute_type, foreign_key: 'attribut_type'
end
attribute_type.rb:
class AttributeType < ActiveHash::Base
include ActiveHash::Associations
has_many :attributes, foreign_key: 'attribut_type'
self.data = [
{id: 0, name: 'Text', type: 'text'},
{id: 1, name: 'Date', type: 'date'},
{id: 2, name: 'Number', type: 'number'}
]
end
routes.rb:
Rails.application.routes.draw do
resources :attributes do
collection do
get 'attribute_type'
end
end
end
未完成什么或不足以用“ attribute_type”模型中的单词替换数字的内容?
当我尝试浏览该URL /attributes/attribute_type
时,会发生此错误:
AbstractController :: ActionNotFound(操作'attribute_type' 找不到AttributesController):