[英]How to solve no method error in ruby on rails?
我正確定義了我的路線和 controller 動作。 我仍然收到無方法錯誤。
這是我的代碼:
路線:
get "/restaurants", to: "restaurants#index"
Controller 動作:
def index
resto = Restaurant.all
render json: resto,status: :ok
end
這是我在瀏覽器中的錯誤:
NoMethodError in RestaurantsController#index
undefined method `type' for #<Restaurant:0x00007fb711a07628>
Extracted source (around line #4):
def index
resto=Restaurant.all
( render json: resto,status: :ok) # this is the line throwing error
end
end
這是控制台中的問題
Use Ctrl-C to stop
Started GET "/restaurants" for ::1 at 2022-08-16 22:26:25 +0530
(0.7ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by RestaurantsController#index as HTML
Restaurant Load (1.2ms) SELECT "restaurants".* FROM "restaurants"
↳ app/controllers/restaurants_controller.rb:4:in `index'
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (12.1ms)
Completed 500 Internal Server Error in 38ms (ActiveRecord: 7.5ms | Allocations: 17269)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.