#<#:0x39b05f8> 的未定义局部变量或方法“articles_path” 代码是
<h1>New Article</h1>
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
我是 ruby on rails 编程的新手。当我在 rails server 中运行上面的代码时,我在文章#new 中收到了 NameError 错误。请给我解决上述错误的答案。
Controller 代码
class ArticlesController < ApplicationController
def new
end
def create
render plain: params[:article].inspect
end
end
rake 路线
Prefix Verb URI Pattern Controller#Action
articles_new GET /articles/new(.:format) articles#new
article_index GET /article(.:format) article#index
POST /article(.:format) article#create
new_article GET /article/new(.:format) article#new
edit_article GET /article/:id/edit(.:format) article#edit
article GET /article/:id(.:format) article#show
PATCH /article/:id(.:format) article#update
PUT /article/:id(.:format) article#update
DELETE /article/:id(.:format) article#destroy
root GET / welcome#index
最佳答案
我遇到了这个问题并意识到这是因为我的 routes.rb 有
resources :article
改成
resources :articles
这应该可以解决问题。
关于ruby-on-rails - 未定义的局部变量或方法 `articles_path',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23701094/