简体   繁体   中英

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

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

country_controller.rb looks like:

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:

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

and _form.html.erb is

    <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'? 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.

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

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:

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

Plus anything else you had, the change is: resources :countries should be plural

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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