简体   繁体   English

我在/ posts / post / edit中收到以下错误ActiveRecord :: RecordNotFound找不到带有'id'= post的帖子

[英]I get the following error ActiveRecord::RecordNotFound at /posts/post/edit Couldn't find Post with 'id'=post

I am building a blog and have implemented an edit post function but get the following error message: ActiveRecord::RecordNotFound at /posts/post/edit Couldn't find Post with 'id'=post I am using rails 5 我正在构建博客并实现了编辑帖子功能,但收到以下错误消息:/ posts / post / edit中的ActiveRecord :: RecordNotFound无法找到带有'id'= post的帖子我正在使用rails 5

Thank you in advance. 先感谢您。

**Index.html.erb**
<div class="banner-container">
 <div class="container">
    <div class="banner-gradient-shadow"></div>
      <div class="banner-content banner-font">
        <h1>
          Some text
        </h1>
        <p>
          <strong>More text</strong>.
        </p>
      </div>
    </div>
  </div>
 </div>

 <div class="container">
  <div class="row">
   <% @posts.each do |p| %>
   <%= render "card", post: p %>
   <% end %>
  </div>
</div>

Card partial 卡局部

<div class="container">
 <div class="row">
  <div class="col-xs-12">
   <div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)), url('post.photo_url');">
    <div class="card-category"><%= post.created_at.strftime("Posted %a %m %b %Y at %I:%M%p")  %></div>
      <div class="card-description">
        <h2><%= post.title %></h2>
        <p><%= post.summary %></p>
      </div>
      <!-- <img class="card-user avatar" src="user.jpg"> -->
      <%= link_to "", post_path(post), class: "card-link"%>
    </div>
     <% if current_user %>
     <%= link_to "Edit", edit_post_path(:post), :class => "btn btn-    default" %>
     <% end %>
  </div>
</div>

edit.html.erb edit.html.erb

<div class="container">
 <div class="row">
  <div class="col-md-6 col-md-offset-3">
   <h1>Editing post</h1>
   <%= simple_form_for :post do |f| %>
    <%= f.input :title %>
    <%= f.input :summary %>
    <%= f.input :post_content, as: :text %>
    <%= f.submit class: "btn btn-danger"%>
  <% end %>
  </div>
</div>

posts_controller.rb posts_controller.rb

class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update]
skip_before_action :authenticate_user!, only: [ :edit]

 def index
 @posts = Post.order("created_at DESC").all
 end

def show
@comment = Comment.new
end

def new
@post = Post.new
end

def edit

end

def create
@post = Post.new(post_params)
 if @post.save
 redirect_to post_path(@post)
 else
 render :show
end
end

def update
  if @post.update(post_params)
  redirect_to @post
  else
  render :show
  end
 end


def destroy
end

def set_post
@post = Post.find(params[:id])
end

def post_params
params.require(:post).permit(:title, :post_content, :summary, photo: [], video: [])
end
end

routes.rb routes.rb

Rails.application.routes.draw do
devise_for :users
mount Attachinary::Engine => "/attachinary"
root to: 'posts#index'
resources :posts, only: [:index, :show, :new, :create, :edit, :update] do
resources :comments, only: [:show, :edit, :update, :create, :destroy]
  end
end

Change :post to post in card partial 更改:postpost卡部分

<%= link_to "Edit", edit_post_path(post), :class => "btn btn-default" %>

Rails is sending :post as value of :id like {'id' => ':post'} 轨道被发送:post作为价值:id等{“ID” =>“:交”}

And set_params is trying to find it with params[:id] which is ':post' set_params试图使用params[:id]来找到它,即':post'

@post = Post.find(:post)

I have solved this issue with the changes below: 我已通过以下更改解决了此问题:

index.html.erb no change index.html.erb 不变

card partial 卡偏

<div class="container">
 <div class="row">
  <div class="col-xs-12">
   <div class="card" style="background-image: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.2)), url('post.photo_url');">
    <div class="card-category"><%= post.created_at.strftime("Posted %a %m %b %Y at %I:%M%p")  %></div>
      <div class="card-description">
        <h2><%= post.title %></h2>
        <p><%= post.summary %></p>
      </div>
      <%= link_to "", post_path(post), class: "card-link"%>
    </div>
     <% if current_user %>
     <%= link_to "Edit", edit_post_path(post), :class => "btn btn-default" %>
   <% end %>
   </div>
  </div>
</div>

edit.html.erb edit.html.erb

<div class="container">
 <div class="row">
  <div class="col-md-6 col-md-offset-3">
   <h1>Editing post</h1>
       <%= simple_form_for @post do |f| %>
        <%= f.input :title, placeholder: "Enter a title" %>
        <%= f.input :summary, placeholder: "Short summary of post" %>
        <%= f.input :post_content, as: :text %>
        <%= f.submit class: "btn btn-danger"%>
       <% end %>
   </div>
 </div>
</div>

暂无
暂无

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

相关问题 ActiveRecord::RecordNotFound in CommentsController#create,找不到带有“id”= 的帖子 - ActiveRecord::RecordNotFound in CommentsController#create, Couldn't find Post with 'id'= ActiveRecord::RecordNotFound in PostsController#show, 找不到带有 &#39;id&#39;=# 的帖子 - ActiveRecord::RecordNotFound in PostsController#show, Couldn't find Post with 'id'=# 为什么我得到这个`ActiveRecord :: RecordNotFound。 (找不到&#39;id&#39;)`我的Rails API中的POST请求出错? - Why am I getting this `ActiveRecord::RecordNotFound. (Couldn't find 'id') ` error from POST requests in my Rails API? 我收到以下错误找不到带有&#39;id&#39;=的帖子,并且找不到解决方法? - I am getting the following error Couldn't find Post with 'id'= and can't find a way around it? 错误ActiveRecord :: RecordNotFound(在没有ID的情况下找不到Planner): - Error ActiveRecord::RecordNotFound (Couldn't find Planner without an ID): Rails 4错误:ActiveRecord :: RecordNotFound找不到&#39;id&#39;= - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find 'id'= Rails 4错误:ActiveRecord :: RecordNotFound找不到&#39;id&#39; - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find 'id' Rails 4错误:ActiveRecord :: RecordNotFound找不到id = - Rails 4 Error: ActiveRecord::RecordNotFound Couldn't find id= Rails 4错误:ActiveRecord :: RecordNotFound /找不到&#39;id&#39;= - Rails 4 Error: ActiveRecord::RecordNotFound / Couldn't find 'id'= Ruby日志中的错误-&gt; ActiveRecord :: RecordNotFound(找不到&#39;id&#39;= 1的计划): - Error in Ruby logs -> ActiveRecord::RecordNotFound (Couldn't find Plan with 'id'=1):
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM