ruby-on-rails – 未定义的方法_path(NoMethodError)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 未定义的方法_path(NoMethodError)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我尝试访问包含创建帖子的表单的页面时,我在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 %>

这是Home控制器:

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

解决方法

您需要添加资源:routes.rb文件中的帖子,以便Rails自动为您创建posts_path帮助程序.

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

猜你在找的Ruby相关文章