Ruby on rails 在我的应用程序中添加注释时出现路由错误
我已经完成了Michael Hartl的《Ruby on教程》一书,现在我尝试向MicroPost添加评论,但当我尝试发布评论时,我出现了一个错误:Ruby on rails 在我的应用程序中添加注释时出现路由错误,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,comments,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Comments,我已经完成了Michael Hartl的《Ruby on教程》一书,现在我尝试向MicroPost添加评论,但当我尝试发布评论时,我出现了一个错误: No route matches [POST] 我找不到哪里出了问题 我还想知道我的模型之间的关联是否正确?(我想在帖子上创建类似Facebook的评论) 财务主任评论 class CommentsController < ApplicationController before_action :logged_in_user, only
No route matches [POST]
我找不到哪里出了问题
我还想知道我的模型之间的关联是否正确?(我想在帖子上创建类似Facebook的评论)
财务主任评论
class CommentsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
user = User.find_by(params[:id])
micropost = user.microposts.build(params[:micropost])
@comment = Comment.new(params[:comment])
@comment.micropost_id = micropost_id
@comment.user = current_user
if @comment.save
flash[:success] = "Comment created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
end
您可以将的
表单更改为
<%= form_for([micropost, micropost.comments.build]) do |f| %>
这是如何创建的示例
我希望这对您有所帮助这似乎过于复杂了。创建嵌套资源的方法很简单,如下所示:
<%= form_for([micropost, micropost.comments.build]) do |f| %>
在Rails中嵌套资源路由时,不应使用member do
member
和collection
用于向标准CRUD谓词之外的资源添加其他方法<代码>浅层:true
意味着rails不会嵌套作用于单个资源的“单独”路由,如show、edit、destroy
然后,我们清理控制器:
class CommentsController < ApplicationController
before_action :set_micropost, only: [:index, :new, :create]
# POST /microposts/:micropost_id/comments
def create
@comment = @micropost.comments.build(comment_params) do |c|
c.user = current_user
end
if @micropost.save
# ...
else
# ...
end
end
def comment_params
params.require(:comment).permit(:comment_comment, :picture)
end
def set_micropost
@micropost = Micropost.find(params[:micropost_id])
end
end
class CommentsController
谢谢@max的回答。我试过你的答案,但现在我在_comment.html.erb中遇到另一个问题,系统无法找到“”检查你的模式.rb
,如果你的comments
表有comment
列。否则,您可以通过railsgmigration AddCommentToComments comment:text
创建一个,然后运行rake db:migrate
。我会使用其他名称,因为comment.comment
像comment.body
或者甚至comment.text
。感谢您的链接@Muhamad Akbar Bin Wida。。。那我就试试,我会回到你身边。
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :comment
t.references :user, index: true, foreign_key: true
t.references :micropost, index: true, foreign_key: true
t.timestamps null: false
end
add_index :comments, [:user_id, :micropost_id, :created_at]
end
end
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :micropost
end
class Micropost < ActiveRecord::Base
belongs_to :user
has_many :comments
end
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
end
resources :microposts, only: [:create, :destroy] do
member do
resources :comments, only: [:create, :destroy]
end
end
<%= form_for([micropost, micropost.comments.build]) do |f| %>
<%= form_for(@comment, :url => micropost_comments_path(micropost.id)) do |f| %>
resources :microposts, only: [:create, :destroy] do
resources :comments, only: [:create, :destroy]
end
<%= form_for([micropost, micropost.comments.build]) do |f| %>
resources :microposts, shallow: true, only: [:create, :destroy] do
resources :comments, only: [:create, :destroy]
end
class CommentsController < ApplicationController
before_action :set_micropost, only: [:index, :new, :create]
# POST /microposts/:micropost_id/comments
def create
@comment = @micropost.comments.build(comment_params) do |c|
c.user = current_user
end
if @micropost.save
# ...
else
# ...
end
end
def comment_params
params.require(:comment).permit(:comment_comment, :picture)
end
def set_micropost
@micropost = Micropost.find(params[:micropost_id])
end
end