简体   繁体   English

Rails:路由错误,注释模型到微博模型

[英]Rails: Routing Error, comment model to micropost model

I made a commenting system and I am trying to get it to post under a micropost but I constantly get this routing error. 我制作了一个评论系统,试图将其发布到微博下,但我不断遇到此路由错误。 Any suggestions? 有什么建议么? All help is much appreciated! 非常感谢所有帮助!

Routing Error

No route matches [POST] "/microposts/comments"

Form 形成

<div class="CommentField">
<%= form_for ([@micropost, @micropost.comments.new]) do |f| %>
<%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
<div class="CommentButtonContainer">
<%= f.submit "Comment", :class => "CommentButton b1" %>
</div>
<% end %>
</div>

comment controller 评论控制器

class CommentsController < ApplicationController 
  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = @micropost.comments.build(params[:comment])
    @comment.user_id = current_user.id
    @comment.save 
      respond_to do |format|
      format.html 
      format.js
    end
  end

end

routes 路线

resources :microposts do
  resources :comments
end

Micropost Model 微邮模型

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content, :view_count
  acts_as_voteable
  belongs_to :user
  has_many :comments
  has_many :views
  accepts_nested_attributes_for :comments
end

User Controller 用户控制器

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @school = School.find(params[:id])
    @micropost = Micropost.new
    @comment = Comment.new
    @comment = @micropost.comments.build(params[:comment])
    @microposts = @user.microposts.paginate(:per_page => 10, :page => params[:page])
  end
end

The reason you are getting the error is that you are trying to build a form for a comments of a micropost that does not exist yet in the database. 出现此错误的原因是,您正在尝试为数据库中尚不存在的micropostcomments构建表单。

the form, there is a - 表格中有-

   form_for ([@micropost, @micropost.comments.new]) do |f|

And in UsersController you have - 在UsersController中,您可以-

  @micropost = Micropost.new

comment is a sub-resource of micropost, so a url that creates a comment should look like /micropost/:id/comments where :id is the id of micropost. comment是micropost的子资源,因此创建评论的url应该看起来像/micropost/:id/comments ,其中:id是micropost的ID。 That is possible only after the micropost is saved. 只有在保存微职位后才有可能。

So I believe your action should assign @micropost to an existing post, or create one right there to have the form working. 因此,我认为您的操作应将@micropost分配给现有帖子,或在其中创建一个以使表格正常工作。 Something like - 就像是 -

   @micropost = Micropost.last || Micropost.create

would at least get rid of the error. 至少会消除错误。

I'll try this again (deleted my other answer since, as Marc Talbot pointed out, was not the correct response to your issue) . 我将再次尝试(删除其他答案,因为正如Marc Talbot所指出的那样,这不是对您问题的正确答案)

Perhaps the issue is as simple as making :microposts be :micropost instead (to reflect your model's name) . 也许问题就像将:micropost改为:microposts一样简单(以反映您模型的名称)

resources :micropost do
  resources :comments
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM