Ask A Question

Notifications

You’re not receiving notifications from this thread.

NoMethodError in Contacts#edit

Don Neethling asked in Rails
Hi all, I am new to Ruby as well as Rails
I am working my way through a treehouse course and I have hit a bit of an obstacle.

This is what my routes file looks like
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get '/contacts/:id/edit', to: 'contacts#edit'
  get '/contacts', to: 'contacts#index', as: 'home'
  get '/contacts/new', ato: 'contacts#new', as: 'new'
  get '/contacts/:id', to: 'contacts#show', as: 'shows'
  post '/contacts', to: 'contacts#create'
end

the method in the controller is as follows
    def edit
        @contact = Contact.find(params[:id])
    end
and this is what the edit.html.erb file looks like
<%= form_for(@contact) do |c| %>
    <div>
        <%= c.label 'First Name' %>
        <%= c.text_field :fname %>
    </div>
    <div>
        <%= c.label 'Last Name' %>
        <%= c.text_field :lname %>
    </div>  
    <div>
        <%= c.label :number %>
        <%= c.text_field :number %>
    </div>  

    <div>
        <%= c.submit %>
    </div>
<% end %>

as is this will result in the following when I I try to load the edit page (example: http://localhost:3000/contacts/1/edit)


NoMethodError in Contacts#editShowing 

/Users/donovanneethling/Ruby/addressbook/app/views/contacts/edit.html.erbwhere line #1raised:

undefined method `contact_path' for #<#<Class:0x007fca54438cf0>:0x007fca57e82a78>
Did you mean?  contacts_path




However when I amend the edit route as follows
get '/contacts/:id/edit', to: 'contacts#edit', as: 'contact'
 the page loads correctly.

Would someone be able to explain this to me, I am guessing it has something to do with form_for...


Reply
As @jack mentioned, it's probably better using resources.  This should fix things.  If it doesn't please post your error with any potential stacktrace and the community will have a look.
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.