Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined method _path (NoMethodError)

I'm receiving the following error in my Rails app when I try to access a page that contains a form to create a post. I'm trying to implement a feature similar to Michael Hartl's Micropost feature in his sample app:

NoMethodError in Home#index
undefined method `posts_path' for #<#<Class:0xb5c70744>:0xb60013b8>

Here's the index view page that contains the code to insert the form:

<%= render 'shared/post_form' if user_signed_in? %>

_post_form.html.erb:

<%= form_for(@post) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Provide your network with a status update..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

Here is the Home controller:

    class HomeController < ApplicationController

    before_filter :authenticate_user!

  def index
    #render :text => "Welcome #{current_user.email}!"
    @users = User.all
    @post = current_user.posts.build if signed_in?
  end
end

I can really use some help in reviewing the code. I'm staring at it and I need someone else to review it for me. I'm new to Rails so please forgive me if I did not provide the necessary information.

Additional information: I'm using the Devise gem to handle user authentication.

Thanks!

EDIT: I added the wrong controller.

EDIT 2:

Routes.rb file:

AppName::Application.routes.draw do

  #get "users/index"

  #get "users/show"



authenticated :user do
    root :to => 'home#index'
  end
  root :to => "home#index"
  devise_for :users
  resources :users do
    member do
      get :following, :followers, :posts
    end
  end
  resources :works
  resources :relationships, only: [:create, :destroy]
end

EDIT 3: Rake routes

root        /                              home#index
                    root        /                              home#index
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
          following_user GET    /users/:id/following(.:format) users#following
          followers_user GET    /users/:id/followers(.:format) users#followers
              posts_user GET    /users/:id/posts(.:format)     users#posts
                   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
                         PUT    /users/:id(.:format)           users#update
                         DELETE /users/:id(.:format)           users#destroy
                   works GET    /works(.:format)               works#index
                         POST   /works(.:format)               works#create
                new_work GET    /works/new(.:format)           works#new
               edit_work GET    /works/:id/edit(.:format)      works#edit
                    work GET    /works/:id(.:format)           works#show
                         PUT    /works/:id(.:format)           works#update
                         DELETE /works/:id(.:format)           works#destroy
           relationships POST   /relationships(.:format)       relationships#create
            relationship DELETE /relationships/:id(.:format)   relationships#destroy
like image 583
winston Avatar asked Mar 08 '13 18:03

winston


1 Answers

You need to add resources :posts in your routes.rb file in order for Rails to automatically create the posts_path helper for you.

Adding resources :posts will generate the proper RESTful routes for you to create, delete, update, and fetch posts. Take a look at the Ruby on Rails Guide for routing, specifically this section here on routing and RESTful routes.

like image 85
Zajn Avatar answered Nov 10 '22 10:11

Zajn