"Undefined_method, вы имели в виду [множественный] путь?" сообщение об ошибке в form_with Rails 5.1.6, используя ресурсы - PullRequest
0 голосов
/ 17 мая 2018

Я уверен, что это довольно простой вопрос, но я несколько дней смущающе бился головой об эту кирпичную стену. Итак, у меня есть модель под названием «Страна».

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, я получаю сообщение об ошибке, говорящее, что 'country_path' не определен, и я имел в виду 'country_path'? И я абсолютно заблудился относительно того, где я ошибся здесь.

Ура! Mike

Ответы [ 2 ]

0 голосов
/ 17 мая 2018

Речь идет о плюрализации. 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

Кроме того, я знаю, что это не является частью вашего вопроса, но, возможно, вы захотите обновить действие по созданию:

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
0 голосов
/ 17 мая 2018

На routes.rb вы должны иметь:

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

Плюс ко всему, что у вас было, изменение: resources :countries должно быть во множественном числе

...