【问题标题】:I get the following error ActiveRecord::RecordNotFound at /posts/post/edit Couldn't find Post with 'id'=post我在 /posts/post/edit 处收到以下错误 ActiveRecord::RecordNotFound 找不到带有 'id'=post 的帖子
【发布时间】:2016-09-14 09:50:24
【问题描述】:

我正在建立一个博客并实现了编辑帖子功能,但收到以下错误消息:ActiveRecord::RecordNotFound at /posts/post/edit 找不到带有 'id'=post 的帖子我正在使用 rails 5

提前谢谢你。

**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>

卡部分

<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

<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

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

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

【问题讨论】:

  • 你试过&lt;%= link_to "Edit", edit_post_path(post.id), :class =&gt; "btn btn-default" %&gt; 吗?

标签: ruby-on-rails ruby activerecord


【解决方案1】:

部分卡片

中将:post更改为post
<%= link_to "Edit", edit_post_path(post), :class => "btn btn-default" %>

Rails 发送 :post 作为 :id 的值,例如 {'id' => ':post'}

set_params 正试图用params[:id] 找到它,即':post'

@post = Post.find(:post)

【讨论】:

  • 我确实尝试过。出于某种原因,当我删除 set_post 操作以进行编辑时,控制器似乎出错了,错误发生并让我进入编辑表单,但是当我将数据添加到表单时,我再次收到 sam 错误。希望这是有道理的
  • 是的,我这样做无济于事。渲染部分时 index.html.erb 有问题吗?
  • 啊.. 是的,您将p 作为语言环境传递并使用post,因此您需要在您使用过的部分卡中将post 更改为p
  • 而且我相信您在post.title 中也一定遇到了错误
  • 我的标题没有问题,将帖子更改为 p 编辑仍然有同样的错误。我想我会尝试将编辑链接移动到显示操作中并使用显示参数。
【解决方案2】:

我已经通过以下更改解决了这个问题:

index.html.erb 没有变化

卡片部分

<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

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多