【问题标题】:Error with Simple Action Controller简单动作控制器出错
【发布时间】:2016-10-12 04:09:26
【问题描述】:

我是 Ruby on Rails 的新手,正在尝试建立一个超级简单的数据库。

我得到了一个

ProfilesController#index 中的 ActionController::UnknownFormat

ProfilesController#index 缺少此请求格式和变体的模板。 request.formats: ["text/html"] request.variant: [] 注意!对于 XHR/Ajax 或 API 请求,此操作通常会响应 204 No Content:一个空白屏幕。由于您是在 Web 浏览器中加载它,我们假设您希望实际呈现一个模板,而不是……什么都没有,所以我们显示一个错误以非常清楚。如果您期望 204 No Content,请继续。这就是您将从 XHR 或 API 请求中获得的信息。试一试。

错误,我不确定发生了什么。我尝试研究,但我不知道我做错了什么。

#profiles_controller.rb:

class ProfilesController < ApplicationController
  def index
    @profiles = Eprofiles.all
  end
end


#routes.rb:

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get 'welcome', to: 'pages#home'
  get 'profiles', to: 'profiles#index'
end

#eprofiles.rb

class Eprofiles < ApplicationRecord
end

【问题讨论】:

  • 可以是EProfile.all吗?模型是单数,而不是复数是 Rails 约定

标签: ruby-on-rails ruby


【解决方案1】:

错误是不言自明的

ProfilesController#index is missing a template for this request format and variant. request.formats: ["text/html"]

这意味着您没有文件app/views/profiles/index.html.erb

【讨论】:

  • 谢谢!我有文件,但路径不正确。
【解决方案2】:

查看app/models,然后找到文件eprofile.rb

在该文件中,我想类名是class Eprofile。您想使用指出的 /u/Ren 之类的单数名称。

在您的路线中,请使用 rails 指南中建议的格式

get 'welcome', to: 'pages#home'
get 'profiles', to: 'profiles#index'

请务必阅读http://guides.rubyonrails.org/routing.html。一些最好的文档!

【讨论】:

  • 您好!感谢您的回复,我一定会查看那些文档,我一直在从 Code Academy 学习。我已经编辑了我的代码以匹配您的建议,但我仍然遇到同样的错误!
  • 错误告诉你这个问题到底是什么。您需要相关的模板。同样,指南非常详细地概述了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 2019-04-15
  • 1970-01-01
相关资源
最近更新 更多