Reputation: 3132
I have my routes defined as below
get 'contacts/new', to: 'contacts#new'
And in my ContactsController I have defined as below
def new
@contact = Contact.new
end
In views contacts/new/_form.html.erb I have structured form as below
<%= form_for @contact, html: {multipart:true} do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<% end %>
But when i go to localhost:3000/contacts/new
I get the below error.
undefined method contacts_path which is occuring in first line of form.
But when i try to define as below in routes, it worked
get 'contacts/new', to: 'contacts#new', as: 'contact'
Any idea why rails throws such error when i have defined it in the routes file. I am just trying to understand inner workings of rails routes
Upvotes: 0
Views: 132
Reputation: 931
Make a post type route for contacts (for this it is throwing error)
Or remove this route
get 'contacts/new', to: 'contacts#new'
And add this simply
resources :contacts
Upvotes: 1
Reputation: 6321
Try better use railsy way like resources
as @Graham mentioned.
or
get 'contacts', to: 'contacts#index', as: :contacts #genetares: contacts_path
get 'contacts/new', to: 'contacts#new', as: :new_contact #new_contact_path
Upvotes: 2
Reputation: 6870
To avoid this kind of errors, remove your route and use:
resources :contacts, only: [:new, :create]
Upvotes: 2