[英]ActiveRecord::RecordNotFound in CommentsController#create, Couldn't find Post with 'id'=
After clicking on create comment, i got error like this点击创建评论后,我收到这样的错误
coment controller:评论控制器:
` I used FriendlyId instead of post id. ` 我使用了 FriendlyId 而不是 post id。
error:错误:
ActiveRecord::RecordNotFound in CommentsController#create Couldn't find Post with 'id'= ActiveRecord::RecordNotFound in CommentsController#create 找不到带有 'id'= 的帖子
class CommentsController < ApplicationController类 CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, :body))
redirect_to post_path(@post)
end
comments form: _comments.html.erb
`<%= form_for([@post,@post.comments.build]) do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>`
posts controller: post_controller.rb
`class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all.order("created_at DESC")
end
def show
end
def new
@post = Post.new
end
def edit
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
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
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_post
@post = Post.friendly.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :description, :image)
end
end
`
model: post.rb
class Post < ApplicationRecord
has_many :comments
extend FriendlyId
friendly_id :title, use: :slugged
has_attached_file :image, styles: { medium: "900x380#", thumb: "300x165>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
Do:做:
@post = Post.friendly.find(params[:post_id])
After you add this, add a new column to your database table by running添加后,通过运行向数据库表中添加一个新列
rails g migration AddSlugToPosts slug:uniq
After you have done this, locate your post model and add:完成此操作后,找到您的帖子模型并添加:
extend FriendlyId
friendly_id :first_name, use: :slugged
Note: I use first_name
for my slug.注意:我使用
first_name
作为我的 slug。 It's up to you which one you want to use, you can read more about how to setup friendly here .由您决定要使用哪个,您可以在此处阅读有关如何设置友好的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.