繁体   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

我敢肯定,这是一个基本的问题,但是令人尴尬的是,我几天来一直在撞墙。 因此,我有一个名为“国家”的模型。

Routes.rb看起来像:

Rails.application.routes.draw do    
  resources :country    
  resources :references
  get 'homepage/home'
end

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是:

<%= render 'form', country: @country %>

_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>

在/ country / new上,我收到一条错误消息,提示未定义“ countries_path”,我的意思是“ country_path”吗? 我绝对不知道我哪里出了错。

干杯! 麦克风

关于多元化。 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

另外,我知道这不是您的问题的一部分,但也许您会希望将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

routes.rb您应该具有:

Rails.application.routes.draw do    
 resources :countries    
end

加上您拥有的所有其他内容,更改是: resources :countries应为复数形式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM