[英]“Undefined_method, did you mean [pluralised] path?” error message on form_with Rails 5.1.6, using resources
Bit of a basic question, I'm sure, but I've been banging my head against this particular brick wall for days, embarrassingly. 我敢肯定,这是一个基本的问题,但是令人尴尬的是,我几天来一直在撞墙。 So, I have a model, named 'Country'.
因此,我有一个名为“国家”的模型。
Routes.rb looks like: Routes.rb看起来像:
Rails.application.routes.draw do
resources :country
resources :references
get 'homepage/home'
end
country_controller.rb looks like: country_controller.rb看起来像:
class CountryController < ApplicationController
before_action :set_country, only: [:show, :edit, :update, :destroy]
def new
@country = Country.new
end
def create
@country = Country.new(:name => params[:name], :metatitle => params[:metatitle], :metadescription => params[:metadescription], :ogtitle => params[:ogtitle], :ogdescription => params[:ogdescription], :abouthtml => params[:abouthtml])
if @country.save
redirect_to country_index_path, notice: 'Country was successfully created.'
else
redirect_to :new, notice: 'Something went wrong :('
end
end
def update
if @country.update
redirect_to country_index_path, notice: 'Country was successfully created.'
else
redirect_to :new, notice: 'Something went wrong :('
end
end
end
new.html is: new.html是:
<%= render 'form', country: @country %>
and _form.html.erb is _form.html.erb是
<div class="form">
<%= form_with(model: country, local: true) do |f| %>
<div class="form_element">
<%= f.label "Name" %>
<%= f.text_field :name %>
</div>
<div class="form_element">
<%= f.label "Meta Title" %>
<%= f.text_field :metatitle %>
</div>
<div class="form_element">
<%= f.label "Meta Description" %>
<%= f.text_field :metadescription %>
</div>
<div class="form_element">
<%= f.label "OG_Title" %>
<%= f.text_field :ogtitle %>
</div>
<div class="form_element">
<%= f.label "OG_Description" %>
<%= f.text_field :ogdescription %>
</div>
<div class="form_element">
<%= f.label "abouthtml" %>
<%= f.text_field "About (HTML)" %>
</div>
<%= f.submit %>
<% end %>
</div>
On /country/new, I'm getting an error saying 'countries_path' isn't defined, and did I mean 'country_path'? 在/ country / new上,我收到一条错误消息,提示未定义“ countries_path”,我的意思是“ country_path”吗? And I'm absolutely lost as to where I've gone wrong here.
我绝对不知道我哪里出了错。
Cheers! 干杯! Mike
麦克风
It's about pluralization. 关于多元化。
routes
and controllers
are pluralized, model
in singular. routes
和controllers
都使用复数model
中单数。
# config/routes.rb
Rails.application.routes.draw do
resources :countries # <= HERE
resources :references
get 'homepage/home'
end
# apps/controllers/countries_controller.rb
class CountriesController < ApplicationController
# ...
end
Also, I know is not part of your question, but maybe you will want to update your create action to: 另外,我知道这不是您的问题的一部分,但也许您会希望将create动作更新为:
def create
@country = Country.new(country_params)
if @country.save
redirect_to countries_path, notice: 'Country was successfully created.'
else
redirect_to :new, notice: 'Something went wrong :('
end
end
protected
def country_params
params.require(:country).permit(:name, :metatitle, :metadescription, :ogtitle, :ogdescription)
end
On routes.rb
you should have: 在
routes.rb
您应该具有:
Rails.application.routes.draw do
resources :countries
end
Plus anything else you had, the change is: resources :countries
should be plural 加上您拥有的所有其他内容,更改是:
resources :countries
应为复数形式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.