【问题标题】:ActiveRecord::RecordNotFound in PostsController#edit "Couldn't find Place with 'id'=17"ActiveRecord::RecordNotFound in PostsController#edit "找不到 'id'=17 的地方"
【发布时间】:2016-05-07 20:06:54
【问题描述】:

我在编辑帖子时遇到问题。当我单击编辑并且假设将我重定向到要编辑的页面时,我得到一个错误。我有两张桌子,一张是posts,另一张是places。我的places 表中有一个名为post_id 的外键。每个帖子has_one 地方。我知道我在PostController 中使用“place”进行的“编辑”没有被正确调用,因为它可能需要被引用。我该怎么做才能使代码正常工作??

PostsController:

class PostsController < ApplicationController

before_action :authenticate_user!, :except => [:show, :index, :new]

before_action :set_post, only: [:show, :edit, :update, :destroy]

before_action :owned_post, only: [:edit, :update, :destroy]  

  def index
    @post = Post.new
    @posts = Post.all
  end

  def show

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

  def new

    @post = current_user.posts.build

    @posts = Post.all


  end

  def create
    @post = current_user.posts.build(post_params)


    if @post.save
      flash[:success] = "Your post has been created!"
      redirect_to root_path
    else
      flash[:alert] = "Your new post couldn't be created!  Please check the form."
      render :new
    end
  end

  def find
    @place = Place.new
  end

  def edit
    @post = Post.find(params[:id])
    @place = Place.find(params[:id])
  end

  def update
     if @post.update(post_params)
      flash[:success] = "Post updated."
      redirect_to root_path
    else
      flash.now[:alert] = "Update failed.  Please check the form."
      render :edit
    end
  end

  def destroy
    @post.destroy

    flash[:success] = "Your Post has been removed."
    redirect_to root_path
  end

  private



  def post_params
    params.require(:post).permit(:image, :caption, :latitude, :longitude)
  end

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

  def owned_post  
  unless current_user == @post.user
    flash[:alert] = "That post doesn't belong to you!"
    redirect_to root_path
  end
end  

end

我还想在创建新帖子时向此表单添加地点,以便地点和帖子表都填充彼此相关的正确字段。

创建帖子的表单:

<%= form_image_select(@post) %>
        <%= simple_form_for @post, html: { multipart: true } do |f| %>
          <div class="row">

            <div class="col-md-12 text-center">
              <%= f.error_notification %>
            </div>
          </div>
          <div class="container-fluid">
            <div class="form-group text-center">
              <h4>Upload an image (this is required):</h4>
              <%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %>
            </div>
            <%= simple_fields_for place.new do |o| %>
            <div class="form-group text-center">
            <%= o.input :address, label: false, placeholder: "search", class: 'controls', id: "pac-input" %>
           <input id="pac-input" class="controls" type="text" placeholder="Search Box">

            </div>


          <% end %>

            <div class="form-group text-center">
              <%= f.input :caption, label: false, placeholder: 'Add your caption' %>
            </div>
            <div class="form-group text-center">
              <%= f.button :submit, class: 'btn-success btn-block' %>
            </div>
          </div>
        <% end %>

新的错误日志:

于 2016-05-08 05:59:33 +0900 开始为 ::1 获取“/posts/17/edit” PostsController#edit 处理为 HTML 参数:{"id"=>"17"} 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 16]] Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ?限制 1 [["id", 17]] 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?限制 1 [["id", 16]] Place Load (0.1ms) SELECT "places".* FROM "places" WHERE "places"."post_id" = ?限制 1 [[“post_id”,17]] 渲染的帖子/_form.html.erb (14.0ms) 在布局/应用程序中渲染的帖子/edit.html.erb (14.9ms) 在 21 毫秒内完成 500 内部服务器错误(ActiveRecord:0.3 毫秒)

ActionView::Template::Error(未定义的局部变量或方法place' for #<#<Class:0x007f92b27c7738>:0x007f92bb24b288>): 17: <h4>Upload an image (this is required):</h4> 18: <%= f.input :image, label: false, input_html: { onChange: 'loadFile(event)' } %> 19: </div> 20: <%= simple_fields_for place.new do |o| %> 21: <div class="form-group text-center"> 22: <%= o.input :address, label: false, placeholder: "search", class: 'controls', id: "pac-input" %> 23: <input id="pac-input" class="controls" type="text" placeholder="Search Box"> app/views/posts/_form.html.erb:20:inblock in _app_views_posts__form_html_erb__1067044067393355525_70134077243420' app/views/posts/_form.html.erb:8:in _app_views_posts__form_html_erb__1067044067393355525_70134077243420' app/views/posts/edit.html.erb:2:in_app_views_posts_edit_html_erb__4372466127864082923_70134042275460'

【问题讨论】:

  • 您正在查找具有相同 ID 的帖子和地点。它们不一定具有相同的 id。此外,根据您的描述,您应该在帖子中包含 place_id,而不是相反。因为帖子只有一个地方,但一个地方可以有很多帖子。

标签: ruby-on-rails database activerecord controller foreign-keys


【解决方案1】:

在您的编辑操作中,您尝试通过:id(即帖子的 ID)查找您的地点。假设您的 Post 模型中有 has_one :place 关系,您可以这样做:

def edit
  @post = Post.find(params[:id])
  @place = @post.place;
end

设置@post 是多余的,因为您在set_post 中进行了设置,因此您的编辑操作可以变为:

def edit
  @place = @post.place;
end

【讨论】:

  • 哇,我明白了!是的,它摆脱了第一个错误,现在它说:NameError in Posts#edit "undefined local variable or method `place'"
  • 那是因为 post 和 place 模型之间缺少关联/关系。将此添加到 post.rb has_one :place 并在 place.rb 中添加 belongs_to: post
  • 嘿嘿! Iv has_one :placebelongs_to: post 已经参与了模型!还有什么可能导致这种情况?
  • 如果可以的话,你能把完整的错误日志贴在这里吗?
  • 在第 20 行,您使用的是 place,但未定义它。我认为您应该使用@place 而不是place.new
猜你喜欢
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 2017-04-10
  • 1970-01-01
相关资源
最近更新 更多