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