当我尝试访问包含创建文章表单的页面时,我的Rails应用程序会出现以下错误。我正在尝试实现类似于Michael Hartl示例应用程序中的微型文章功能:
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:执行 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
<%= form_for(@post) do |f| %>
修改为<%= form_for(Post.new) do |f| %>
。 - Rahul Tapali<%= form_for Post.new ,:url => {:action => :create, :controller => :posts} do |f| %>
这应该可以工作。 - Rahul Tapali