草庐IT

ruby-on-rails - PinsController#index : declare the formats your controller responds to in the class level 中的运行时错误

coder 2025-06-17 原文

在我的 Rails 应用程序上工作时,我在终端中使用以下命令创建了一个“Pins”脚手架:

rails generate scaffold Pins description:string --skip-stylesheets

这会在我的应用程序中创建脚手架,然后我运行:

rake db:migrate

一切顺利。我没有更改任何生成的页面,但是当我最终尝试访问 localhost:3000 上的新脚手架时,出现以下错误:

RuntimeError in PinsController#index

In order to use respond_with, first you need to declare the formats your controller responds to in the class level Rails.root: /Users/code/appname

Application Trace | Framework Trace | Full Trace

app/controllers/pins_controller.rb:6:in `index'

我一直在按照视频教程创建我的应用程序,但视频中的用户并未提出问题。我正在尝试使用在线资源找出问题,但无法解决我的问题。

你们能帮帮我吗?

谢谢!

最佳答案

在 Controller 的顶部,您需要添加:

class PinsController < ApplicationController
  respond_to :html, :xml, :json
  ...
end

您可以在 API Dock 上阅读有关此 mime 类型的更多信息

关于ruby-on-rails - PinsController#index : declare the formats your controller responds to in the class level 中的运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26345584/

有关ruby-on-rails - PinsController#index : declare the formats your controller responds to in the class level 中的运行时错误的更多相关文章

随机推荐