简体   繁体   English

使用资源在form_with Rails 5.1.6上显示“ Undefined_method,您的意思是[复数]路径吗?”错误消息

[英]“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. routescontrollers都使用复数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.

相关问题 Rails 4销毁action,undefined_method message_path - Rails 4 destroy action, undefined_method message_path Rails脚手架特定表的奇怪的undefined_method错误 - Rails scaffold strange undefined_method error for specific table Rails Form_with 显示内联错误消息 - Rails Form_with display inline error message 如何调试未定义的方法form_with错误 - How to debug undefined method form_with error rails NoMethodError(“1”的未定义方法`map':String您是说吗?点击): - rails NoMethodError (undefined method `map' for “1”:String Did you mean? tap): Rails 错误:NoMethodError:Capistrano::Configuration:Class 的未定义方法`instance&#39; 你的意思是? 实例的? - Rails Error: NoMethodError: undefined method `instance' for Capistrano::Configuration:Class Did you mean? instance_of? 如何在rails中使用嵌套资源时修改form_with - how to modify form_with when using nested resources in rails has_many form_with 中未定义的方法“networkinterface_path” - Undefined method `networkinterface_path' in a has_many form_with 消息的未定义方法“after_create_commit”:Class 你是说吗? after_create - Rails 动作电缆教程 - Undefined method `after_create_commit' for Message:Class Did you mean? after_create - Rails action cable tutorial 模块未在控制器中加载(undefined_method错误) - module not loading in the controller (undefined_method error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM