【问题标题】:I have a cloud 9 error while running the app in cloud 9在云 9 中运行应用程序时出现云 9 错误
【发布时间】:2017-08-13 23:07:49
【问题描述】:

当我运行我的应用程序时出现此错误 :

(NoMethodError in Articles#new
Showing /home/ubuntu/workspace/yappy/qaucky/app/views/articles/new.html.erb where line #3 raised:

undefined method `model_name' for #<Article:0x007ff8e20e4b50>

提取的源代码(第 3 行附近):

<h1>Create an article</h1>
   <%= form_for @article do |f| %>
   <% end %>

这是图片 (https://i.stack.imgur.com/kghdF.png)

Rails.root:/home/ubuntu/workspace/yappy/qaucky)

资源:文章

root 'pages#home'
get 'about', to: 'pages#about'

我的 new.html.erb 文件:

 <h1>Create an article</h1>
<%= form_for @article do |f| %>
<% end %>

我的文章控制器文件: 类 ArticlesController

def new

@article = Article.new 

end





end 

我的 article.rb 文件:

class Article 


end

我的 routes.rb 文件:

Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest 
priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
resources :articles  

root 'pages#home'
get 'about', to: 'pages#about'

# Example of regular route:
#   get 'products/:id' => 'catalog#view'

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 cloud9-ide cloud9


    【解决方案1】:

    如果 @article 不响应 model_name 以及 Rails 用于确定和生成 URL 的其他几种方法,则不能使用 form_for @article

    鉴于您的问题被标记为ruby-on-rails,我猜您想将文章存储在数据库中。 ActiveRecord::Base 提供了这种方法。只是继承它。改变

    class Article 
    end
    

    class Article < ActiveRecord::Base
    end
    

    注意:这仅适用于您的数据库中有一个名为 articles 的表。

    【讨论】:

    • 我收到一条错误消息:ActiveRecord::StatementInvalid in ArticlesController#new 找不到表 'articles' 它显示我的文章控制器文件:class ArticlesController
    • 错误信息Could not find table 'articles' 非常具体。没有数据库表来存储您使用表单创建的文章。只需创建一个数据库迁移,添加您需要的表和列并运行它。请参阅Rails Guides about migrations
    • 我正在尝试从 UI 制作文章这仍然有效
    • 我是一个 Rails 新手,请你解释清楚一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 2021-12-07
    相关资源
    最近更新 更多