0

StackOverflow ですべての「未定義のメソッド」の質問ソリューションを試したと思いますが、自分の状況に適したソリューションをまだ見つけていません...

「Has」という名前のモデルがあり、私の問題はその末尾の「s」に関連している可能性があると考えています。has/new url を読み込もうとすると、常にこのエラーが発生します。/has は問題なく動作します。ただ新しくない...

#<#:0x007ff3bbaa8d48> の未定義のメソッド `has_index_path'

Has.rb:

class Has < ActiveRecord::Base
  attr_accessible :bathroom_count, :bedroom_count, :neighborhood, :price, :property_type
  belongs_to :trader
end

has_controller.rb:

class HasController < ApplicationController

  def index
    @has = Has.all
  end

  def show
    @has = Has.find(params[:id])
  end

  def new
    @has = Has.new
  end

  def create
    @has = Has.new(params[:has])
    if @has.save
      flash[:success] = "New Listing Created!"
      redirect_to (:back)
    else
      redirect_to (:back)
    end
  end

  def destroy
    @has.destroy
  end
end

ビュー (new.html.erb) (明らかに simple_form を使用しており、それは他の「新しい」ビューでも問題なく機能しています)

<div class="center hero-unit">
<%= simple_form_for (@has) do |f| %>
    <%= f.association :trader%>
    <%= f.input :price %>
    <%= f.input :bathroom_count %>
    <%= f.input :bedroom_count %>
    <%= f.input :property_type %>
    <%= f.input :neighborhood %>
    <%= f.button :submit %>
<% end %>

ルート.rb:

Algotest::Application.routes.draw do
  resources :traders
  resources :wants
  resources :has

  root to: 'static_pages#index'

  match '/add_traders', to: 'traders#new'
  match '/traders', to: 'traders#index'
  match '/add_has', to: 'has#new'
  match '/has', to: 'has#index'
  match '/add_wants', to: 'wants#new'
  match '/wants', to: 'wants#index'
end

編集: rake ルートの出力は次のとおりです。

    traders GET    /traders(.:format)          traders#index
            POST   /traders(.:format)          traders#create
     new_trader GET    /traders/new(.:format)      traders#new
    edit_trader GET    /traders/:id/edit(.:format) traders#edit
         trader GET    /traders/:id(.:format)      traders#show
                PUT    /traders/:id(.:format)      traders#update
                DELETE /traders/:id(.:format)      traders#destroy
          wants GET    /wants(.:format)            wants#index
                POST   /wants(.:format)            wants#create
       new_want GET    /wants/new(.:format)        wants#new
      edit_want GET    /wants/:id/edit(.:format)   wants#edit
           want GET    /wants/:id(.:format)        wants#show
                PUT    /wants/:id(.:format)        wants#update
                DELETE /wants/:id(.:format)        wants#destroy
            has GET    /has(.:format)              has#index
                POST   /has(.:format)              has#create
         new_ha GET    /has/new(.:format)          has#new
        edit_ha GET    /has/:id/edit(.:format)     has#edit
             ha GET    /has/:id(.:format)          has#show
                PUT    /has/:id(.:format)          has#update
                DELETE /has/:id(.:format)          has#destroy
           root        /                           static_pages#index
    add_traders        /add_traders(.:format)      traders#new
                       /traders(.:format)          traders#index
        add_has        /add_has(.:format)          has#new
                       /has(.:format)              has#index
      add_wants        /add_wants(.:format)        wants#new
                        /wants(.:format)            wants#index

Inflectionsコードスニペットを追加した後のEDIT 3 New Route。「Has」ルートは見栄えが良くなりましたが、現在、localhost:3000 ページでこのエラーが発生しています

No route matches {:action=>"show", :controller=>"has"}

新しいルートは次のとおりです。

    traders GET    /traders(.:format)          traders#index
            POST   /traders(.:format)          traders#create
 new_trader GET    /traders/new(.:format)      traders#new
edit_trader GET    /traders/:id/edit(.:format) traders#edit
     trader GET    /traders/:id(.:format)      traders#show
            PUT    /traders/:id(.:format)      traders#update
            DELETE /traders/:id(.:format)      traders#destroy
      wants GET    /wants(.:format)            wants#index
            POST   /wants(.:format)            wants#create
   new_want GET    /wants/new(.:format)        wants#new
  edit_want GET    /wants/:id/edit(.:format)   wants#edit
       want GET    /wants/:id(.:format)        wants#show
            PUT    /wants/:id(.:format)        wants#update
            DELETE /wants/:id(.:format)        wants#destroy
  has_index GET    /has(.:format)              has#index
            POST   /has(.:format)              has#create
    new_has GET    /has/new(.:format)          has#new
   edit_has GET    /has/:id/edit(.:format)     has#edit
        has GET    /has/:id(.:format)          has#show
            PUT    /has/:id(.:format)          has#update
            DELETE /has/:id(.:format)          has#destroy
       root        /                           static_pages#index
add_traders        /add_traders(.:format)      traders#new
                   /traders(.:format)          traders#index
    add_has        /add_has(.:format)          has#new
                   /has(.:format)              has#index
  add_wants        /add_wants(.:format)        wants#new
                   /wants(.:format)            wants#index
4

1 に答える 1

2

リソースの名前に名詞を使用することをお勧めしますが、何らかの理由で本当に現在の名前を保持したい場合は、次のハックを使用できます。 " inflector モジュールを使用しても同じです:

configs/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'has', 'has'
end
于 2012-09-25T19:08:27.983 に答える