我正在学习Rails。我有一个有“想法”的应用程序,它有“评论”
我已使用本指南(https://gorails.com/episodes/comments-with-polymorphic-associations)
创建了评论我正在使用'Ancestry'宝石尝试使用railscasts上的本指南嵌套它们(http://railscasts.com/episodes/262-trees-with-ancestry)
无论如何我得到这个错误“ActionController :: RoutingError(未初始化的常量注释):”
这是我收到错误的地方 - 当我点击“回复”链接时
<h3> Comments </h3>
<% @idea.comments.each do |comment| %>
<div>
<%= comment.body %>
<div class="actions">
<%= link_to "Reply", new_comment_path(:parent_id => comment) %>
</div>
</div>
<% end %>
上述内容正在“Ideas / show.html”页面上呈现
<p id="notice"><%= notice %></p>
<p>
<strong>Description:</strong>
<%= @idea.description %>
</p>
<%= render partial: "comments/comments", locals: {commentable: @idea} %>
<%= render partial: "comments/form", locals: {commentable: @idea} %>
<% if @idea.user == current_user %>
<%= link_to 'Edit', edit_idea_path(@idea) %>
<% end %>
<%= link_to 'Back', ideas_path %>
我想将它们发送到“评论/新”,这与我的表单页面相同
<%= form_for [commentable, Comment.new] do |f| %>
<div class="form-group">
<%= f.hidden_field :parent_id %>
<%= f.text_area :body, class: "form-control", placeholder: "Add a comment here" %>
</div>
<%= f.submit class: "btn btn-primary" %>
<% end %>
Routes.rb
Rails.application.routes.draw do
resources :ideas do
resources :comments, module: :ideas
end
devise_for :users
root 'ideas#index'
get "about" => "pages#about"
get "new_comment" => "comments/new"
end
Comments_controller.rb
class CommentsController < ApplicationController
before_action :authenticate_user!
def new
@comment = Comment.new(:parent_id => params[:parent_id])
end
def create
@comment = @commentable.comments.new comment_params
@comment.user = current_user
@comment.save
redirect_to @commentable, notice: "Your comment was posted"
end
private
def comment_params
params.require(:comment).permit(:body)
end
end
Ideas_controller.rb
class IdeasController < ApplicationController
before_action :set_idea, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :correct_user, only: [:edit, :update, :destroy]
respond_to :html
def index
@ideas = Idea.all
end
def show
end
def new
@idea = Idea.new
@idea.comments.build
respond_with(@idea)
end
def edit
end
def create
@idea = current_user.ideas.build(idea_params)
if @idea.save
redirect_to @idea, notice: "Idea was successfully created."
else
render :action => 'new'
end
end
def update
if @idea.update(idea_params)
redirect_to @idea, notice: "Your idea has been updated"
else
render action: 'edit'
end
end
def destroy
@idea.destroy
redirect_to ideas_url
end
private
def set_idea
@idea = Idea.find(params[:id])
end
def correct_user
@idea = current_user.ideas.find_by(id: params[:id])
redirect_to ideas_path, notice: "You can't edit this" if @idea.nil?
end
def idea_params
params.require(:idea).permit(:description)
end
end
非常感谢任何帮助,谢谢。
编辑_添加了我的路线
idea_comments GET /ideas/:idea_id/comments(.:format) ideas/comments#index
POST /ideas/:idea_id/comments(.:format) ideas/comments#create
new_idea_comment GET /ideas/:idea_id/comments/new(.:format) ideas/comments#new
edit_idea_comment GET /ideas/:idea_id/comments/:id/edit(.:format) ideas/comments#edit
idea_comment GET /ideas/:idea_id/comments/:id(.:format) ideas/comments#show
PATCH /ideas/:idea_id/comments/:id(.:format) ideas/comments#update
PUT /ideas/:idea_id/comments/:id(.:format) ideas/comments#update
DELETE /ideas/:idea_id/comments/:id(.:format) ideas/comments#destroy
ideas GET /ideas(.:format) ideas#index
POST /ideas(.:format) ideas#create
new_idea GET /ideas/new(.:format) ideas#new
edit_idea GET /ideas/:id/edit(.:format) ideas#edit
idea GET /ideas/:id(.:format) ideas#show
PATCH /ideas/:id(.:format) ideas#update
PUT /ideas/:id(.:format) ideas#update
DELETE /ideas/:id(.:format) ideas#destroy
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
PATCH /users/password(.:format) devise/passwords#update
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
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / ideas#index
about GET /about(.:format) pages#about
new_comment GET /new_comment(.:format) comments/new#new_comment