我很抱歉,但我对Rails相当新,我似乎无法理解问题所在。我正在建立一个在线论坛,并希望我的用户不仅能够编辑自己的帖子,还能够编辑他们可能创建的任何评论。我一直得到一个:#<#:0x007fe5b6b0cbd0>的未定义方法`comment_path'。有什么想法吗?
路线:
PostitTemplate::Application.routes.draw do
root to: 'posts#index'
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
resources :users, only: [:create, :edit, :update]
resources :posts, except: [:destroy] do
member do
post 'vote'
end
resources :comments, only: [:create, :edit, :update] do
member do
post 'vote'
end
end
end
resources :categories, only: [:new, :create]
end
我的评论edit.html.erb:
<div class="page-header">
<h2>Update Comment<small> - looks like you need some updating!</small></h2>
</div>
<h3><%= @post.description %></h3>
<%= render 'shared_partials/errors', errors_obj: @comment %>
<div class="well">
<%= form_for @comment do |f| %>
<%= f.text_area :body, :class=> "input", :placeholder=> "Comment goes here", :rows => "6" %>
</br>
<div class="button">
<%= f.submit "Create a comment", class: 'btn btn-primary' %>
</div>
<% end %>
</div>
comments_controller:
class CommentsController < ApplicationController
before_action :require_user
def create
@post = Post.find(params[:post_id])
@comment = Comment.new(params.require(:comment).permit(:body))
@comment.post = @post
@comment.creator = current_user
if @comment.save
flash[:notice] = "Your comment was created!"
redirect_to post_path(@post)
else
render 'posts/show'
end
end
def edit
@comment = Comment.find(params[:id])
@post = Post.find(params[:post_id])
end
def update
@comment = Comment.find(params[:id])
if @comment.update(comment_params)
flash[:notice] = "You updated your comment!"
redirect_to post_comments_path
else
render :edit
end
end
end
rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
register GET /register(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout GET /logout(.:format) sessions#destroy
users POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
vote_post POST /posts/:id/vote(.:format) posts#vote
vote_post_comment POST /posts/:post_id/comments/:id/vote(.:format) comments#vote
post_comments POST /posts/:post_id/comments(.:format) comments#create
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
categories POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
答案 0 :(得分:1)
由于您的评论是嵌套资源,因此您需要将@post
传递给form_for()
的电话。
form_for [@post, @comment] do |f|