undefined方法_path(NoMethodError)

时间:2013-03-08 18:35:47

标签: ruby-on-rails

当我尝试访问包含用于创建帖子的表单的页面时,我在Rails应用程序中收到以下错误。我正在尝试在他的示例应用程序中实现类似于Michael Hartl的Micropost功能的功能:

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

这是索引视图页面,其中包含插入表单的代码:

<%= 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 %>

这是家庭控制器:

    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

我可以在查看代码时使用一些帮助。我正盯着它,我需要别人为我审查。我是Rails的新手,如果我没有提供必要的信息,请原谅我。

其他信息:我正在使用Devise gem来处理用户身份验证。

谢谢!

编辑:我添加了错误的控制器。

编辑2:

Routes.rb文件:

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

编辑3:耙路

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

1 个答案:

答案 0 :(得分:21)

您需要在resources :posts文件中添加routes.rb,以便Rails自动为您创建posts_path帮助程序。

添加resources :posts将生成适当的RESTful路由,供您创建,删除,更新和获取posts。请查看Ruby on Rails指南以获取路由,特别是路由和RESTful路由上的this section here