我已经遇到了2天的问题,我关注了很多关于如何在帖子中添加评论的视频和教程,对于其他人来说,它似乎运作顺畅。
我有4个控制器,用户,帖子,评论和墙,基本上这个网站将是一个Facebook克隆。所以我在墙上显示所有内容index.html.erb
我得到的错误是:
No route matches {:action=>"index", :controller=>"comments", :post_id=>nil} missing required keys: [:post_id]
路线:
Rails.application.routes.draw do
devise_for :users
resources :uploads
resources :users
resources :posts do
resources :comments
end
resources :walls
root 'walls#index'
end
评论控制器:
class CommentsController < ApplicationController
before_action :find_post
def index
@comments = Comment.all
end
def new
@post = Post.find(params[:post_id])
end
def create
@comment = @post.comments.create(params[:comment].permit(:content))
@comment.post_id = @post.id
@comment.user_id = current_user.id
@comment.save
# @comment = @post.comments.create(params[:comment].permit[:content])
# @comment.post_id = @post.id
respond_to do |format|
if @comment.save
format.html { redirect_to root_path }
# format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
# def comment_params
# params.require(:comment).permit(:content)
# end
def find_post
@post = Post.find(params[:post_id])
end
end
帖子控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def new
@post = Post.new
@comment = Comment.new
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
@post.user_id = current_user.id
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
@post.user = current_user
respond_to do |format|
if @post.save
format.html { redirect_to root_path }
# format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
@post.user_id = current_user.id
@post.save
respond_to do |format|
if @post.update(params[:post].permit(:image,:content,:youtube_url))
format.html { redirect_to root_path, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
format.json { respond_with_bip(@post) }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
format.json { respond_with_bip(@post) }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to root_path, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:content, :image, :username, :avatar, :youtube_url,:image_cache)
end
end
我在index.html.erb中为墙控制器索引
呈现表单<%= render "form" %>
表单循环 视图/壁/ _form.html.erb
<div class="comments">
<%= form_for ([@post, @post.comments.build]) do |f| %>
byebug
<%= f.text_area :content, class: 'comments js-auto-size', id: 'alex2' ,:rows => 1 %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
我不明白为什么它找不到post_id但是:/
答案 0 :(得分:0)
由于您的comments
嵌套在路由中的posts
内,您必须将post_id
传递给index
中的CommentsController
class CommentsController < ApplicationController
before_action :find_post
def index
@comments = @post.comments
end
end
# in view
<%= link_to "Comments", post_comments_path(@post) %>
否则,您可以将comments
定义为独立资源,这样您就可以在不通过post_id
Rails.application.routes.draw do
resources :posts
resources :comments
end