0

I think I've tried every single "Undefined Method" question solutions on StackOverflow but still haven't found one that works for my situation...

I have a model named "Has" and I'm thinking that my problem may be related to the "s" on the end of that. I keep getting this error anytime I try to load the has/new url. /has works just fine. Just not new...

undefined method `has_index_path' for #<#:0x007ff3bbaa8d48>

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

view (new.html.erb) (using simple_form, obviously and that's working just fine on other "new" views")

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

routes.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

EDIT: Here's the rake routes output:

    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

EDIT 3 New Route after adding the Inflections code snippet. "Has" routes are looking better but now I'm getting this error on any localhost:3000 page

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

Here are the new routes:

    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
3
  • So I see those "ha" routes.... I'm sure that's the problem. I'm new to rails. What should I do about that? Commented Sep 25, 2012 at 18:44
  • 3
    Named routes like new_ha suggest that Rails naming inflection don't work well with your example. In all the names are very uncommon. You might consider using a Demand model and an Offer model, for which Rails would inflect names and routes correctly. Commented Sep 25, 2012 at 18:59
  • Thanks Thomas. Any advice on modifying the routes with model names as-is? I'm using a pretty complex algorithm that was built for me using these names and I was hoping to not have to change it. Commented Sep 25, 2012 at 19:03

1 Answer 1

2

I would suggest using a noun for the name of your resource, but if for some reason you really want to keep the current name, you could use the following hack: trick rails into thinking that the "singular" and "plural" of "has" are the same using the inflector module:

In configs/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'has', 'has'
end
Sign up to request clarification or add additional context in comments.

7 Comments

It would be better to use nouns though. Tricking a system can turn ugly in a later revision of code.
Ah now I'm getting this error since adding the inflections modification: No route matches {:action=>"show", :controller=>"has"}
Make sure that you see the line has GET /has/:id(.:format) has#show when running rake routes.
That does look to be in there... has GET /has/:id(.:format) has#show
What's the context of the error? Are you attempting to render the same template in your original code or a different one? If different, please post it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.