0

我想我已经尝试过 StackOverflow 上的每一个“未定义方法”问题解决方案,但仍然没有找到适合我情况的解决方案......

我有一个名为“Has”的模型,我认为我的问题可能与最后的“s”有关。每当我尝试加载 has/new url 时,我都会不断收到此错误。/has 工作得很好。只是不是新...

#<#:0x007ff3bbaa8d48> 的未定义方法“has_index_path”

有.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 代码片段后编辑 3 New Route。“有”路线看起来更好,但现在我在任何 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

我建议使用名词作为资源的名称,但如果出于某种原因您真的想保留当前名称,则可以使用以下技巧:trick rails into think that the "singular" and "plural" of "has " 使用变形器模块是相同的:

在 configs/initializers/inflections.rb 中:

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