ruby on rails - ActiveRecord::RecordNotFound in CommentsController#create, Couldn't find Post with 'id'= -
after clicking on create comment, got error this
coment controller:
` used friendlyid instead of post id.
error:
activerecord::recordnotfound in commentscontroller#create couldn't find post 'id'=
class 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]) |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 |format| if @post.save format.html { redirect_to @post, notice: 'post 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 |format| if @post.update(post_params) format.html { redirect_to @post, notice: 'post 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 |format| format.html { redirect_to posts_url, notice: 'post 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
Comments
Post a Comment