rails:路由错误 - 没有路由匹配 [GET]“/posts/new”
rails : Routing Error - No route matches [GET] "/posts/new"
我正在创建一个简单的 CRUD,但使用 create 方法遇到了这个问题。
错误
没有路线匹配[POST]“/usuarios/new”
这是我的控制器:
控制器
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
我的 new.html.reb 文件:
new.html.erb
<%= form_for :article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我的 routes.rb 文件:
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
end
有人可以帮助我吗?
您的 post 的大部分内容在此上下文中都是多余的。似乎您尝试调用 new_usarios_path
,但在这种情况下,您必须在 routes.rb
文件中定义此路由:
resources :usarios
我正在创建一个简单的 CRUD,但使用 create 方法遇到了这个问题。
错误
没有路线匹配[POST]“/usuarios/new”
这是我的控制器:
控制器
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
我的 new.html.reb 文件:
new.html.erb
<%= form_for :article do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
我的 routes.rb 文件:
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :articles
root 'welcome#index'
end
有人可以帮助我吗?
您的 post 的大部分内容在此上下文中都是多余的。似乎您尝试调用 new_usarios_path
,但在这种情况下,您必须在 routes.rb
文件中定义此路由:
resources :usarios