ekr990011 ekr990011 - 2 months ago 7
Ruby Question

Rails having a naming error on a form_for that is causing an unknown path error

The issue is I get this nasty little controller error...
NoMethodError in Contacts#new
undefined method `contacts_path' for #<#:0x0000000124b228>
Did you mean? contact_path

on my view: contact#new

<%= form_for @contact do |f| %>

<div class="col-xs-6 form-group contact-input">
<h1><%= f.label :Feedback %></h1>
<%= f.text_area :text, class: "input-lg form-control", rows: "10" %>

<%= f.submit "Send Feedback", class: "btn btn-primary btn-lg" %>
</div>

<% end %>


controller: contacts_controller.rb

class ContactsController < ApplicationController

def new
@contact = Contact.new
end

def create
@contact = Contact.new

if @contact.save
redirect_to '/'
flash[:success] = "Thanks for the Post!"
else
redirect_to contact_path
flash[:alert] = "Please provide input!"
end
end
end


migration:

class CreateContacts < ActiveRecord::Migration
def change
create_table :contacts do |t|

t.text :text

t.timestamps null: false
end
end
end


The error is complaining about line 3 in the view. My model is contact.rb incase I have a plural issue somewhere, but I really do not think that I do. Any help would be much appreciated. This is rails 4.2... I am also a rails beginner.

I know I don't have the flash even setup yet.

rake routes:

Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
about GET /about(.:format) static_pages#about
news GET /news(.:format) static_pages#news
advertise GET /advertise(.:format) static_pages#advertise
fishing GET /fishing(.:format) static_pages#fishing
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
contact GET /contact(.:format) contacts#new
contact_index GET /contact(.:format) contact#index
POST /contact(.:format) contact#create
new_contact GET /contact/new(.:format) contact#new
edit_contact GET /contact/:id/edit(.:format) contact#edit
GET /contact/:id(.:format) contact#show
PATCH /contact/:id(.:format) contact#update
PUT /contact/:id(.:format) contact#update
DELETE /contact/:id(.:format) contact#destroy
forum_comments GET /forums/:forum_id/comments(.:format) comments#index
POST /forums/:forum_id/comments(.:format) comments#create
new_forum_comment GET /forums/:forum_id/comments/new(.:format) comments#new
edit_forum_comment GET /forums/:forum_id/comments/:id/edit(.:format) comments#edit
forum_comment GET /forums/:forum_id/comments/:id(.:format) comments#show
PATCH /forums/:forum_id/comments/:id(.:format) comments#update
PUT /forums/:forum_id/comments/:id(.:format) comments#update
DELETE /forums/:forum_id/comments/:id(.:format) comments#destroy
forums GET /forums(.:format) forums#index
POST /forums(.:format) forums#create
new_forum GET /forums/new(.:format) forums#new
edit_forum GET /forums/:id/edit(.:format) forums#edit
forum GET /forums/:id(.:format) forums#show
PATCH /forums/:id(.:format) forums#update
PUT /forums/:id(.:format) forums#update
DELETE /forums/:id(.:format) forums#destroy
logout DELETE /logout(.:format) sessions#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new


routes.rb :

Rails.application.routes.draw do
root 'static_pages#home'
get 'about' => 'static_pages#about'
get 'news' => 'static_pages#news'
get 'advertise' => 'static_pages#advertise'
get 'fishing' => 'static_pages#fishing'

get 'signup' => 'users#new'
resources :users

get 'contact' => 'contacts#new'
resources :contact

resources :forums do
resources :comments
end

delete 'logout' => 'sessions#destroy'
resources :sessions, only: [:new, :create]
end

Answer

Change resources :contact to resources :contacts in your routes.rb

Comments