PostsController中的ActiveRecord :: RecordNotFound#edit“无法找到'id'= 17的地方”

时间:2016-05-07 20:06:54

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

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

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开始GET“/ posts / 17 / edit”for :: 1 由PostsController处理#编辑为HTML   参数:{“id”=&gt;“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]]   放置加载(0.1ms)SELECT“places”。* FROM“places”WHERE“places”。“post_id”=?限制1 [[“post_id”,17]]   呈现帖子/ _form.html.erb(14.0ms)   在布局/应用程序中呈现posts / edit.html.erb(14.9ms) 在21ms内完成500内部服务器错误(ActiveRecord:0.3ms)

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

1 个答案:

答案 0 :(得分: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