当我运行我的应用程序时,我收到此错误 :
(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 %>
我的文章控制器文件: class ArticlesController&lt; ApplicationController中
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'
答案 0 :(得分:0)
如果form_for @article
没有回复@article
以及Rails用来确定和生成网址的其他几种方法,则您无法使用model_name
。
鉴于您的问题已标记为ruby-on-rails
,我想您希望将文章存储在数据库中。 ActiveRecord::Base
提供了此方法。只是继承它。变化
class Article
end
到
class Article < ActiveRecord::Base
end
注意:仅当数据库中有一个名为articles
的表时才有效。