ActiveRecord::RecordNotFound 问题 Rails 4

ActiveRecord::RecordNotFound Issue Rails 4

我正在开发一个带有嵌套表单等的 rails 应用程序。在我添加一个按钮以尝试将新行项目添加到表单之前,一切都运行良好。布局是一个用户可以创建一个 GeneralReport,一旦进入 GeneralReport,他们就可以添加一个报告条目 (GenRepEnt),但是当我添加按钮时,出现以下错误。

ActiveRecord::RecordNotFound in GeneralReports::GenRepEntsController#new

EDIT # 1 The functionality works in Console and dose as a result populate in the local app..

有问题的Link?是

    <%= link_to "New Report Entry", new_general_report_gen_rep_ent_path(@gen_rep_ents) %>

GenRepEnts 控制器如下:

class GeneralReports::GenRepEntsController < ApplicationController
  before_action :set_gen_rep_ent, only: [:show, :edit, :update, :destroy]

  # GET /gen_rep_ents
  # GET /gen_rep_ents.json
  def index
    @gen_rep_ents = GenRepEnt.all
  end

  # GET /gen_rep_ents/1
  # GET /gen_rep_ents/1.json
  def show
  end

  # GET /gen_rep_ents/new
  def new
    @general_report = GeneralReport.find(params[:general_report_id])
    @gen_rep_ent = GenRepEnt.new
  end

  # GET /gen_rep_ents/1/edit
  def edit
  end

  # POST /gen_rep_ents
  # POST /gen_rep_ents.json
  def create
    @general_report = GeneralReport.find(params[:general_report_id])
    @gen_rep_ent = GenRepEnt.new(gen_rep_ent_params)
    @gen_rep_ent.general_report_id = @general_report.id

    respond_to do |format|
      if @gen_rep_ent.save
        format.html { redirect_to @general_report, notice: 'General Report Entry was successfully created.' }
        format.json { render :show, status: :created, location: @general_report}
      else
        format.html { render :new }
        format.json { render json: @general_report.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /gen_rep_ents/1
  # PATCH/PUT /gen_rep_ents/1.json
  def update
    respond_to do |format|
      if @gen_rep_ent.update(gen_rep_ent_params)
        format.html { redirect_to @gen_rep_ent, notice: 'General Report Entry was successfully updated.' }
        format.json { render :show, status: :ok, location: @gen_rep_ent }
      else
        format.html { render :edit }
        format.json { render json: @gen_rep_ent.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /gen_rep_ents/1
  # DELETE /gen_rep_ents/1.json
  def destroy
    @gen_rep_ent.destroy
    respond_to do |format|
      format.html { redirect_to gen_rep_ents_url, notice: 'General Report Entry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_gen_rep_ent
      @gen_rep_ent = GenRepEnt.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def gen_rep_ent_params
      params.require(:gen_rep_ent).permit(:time, :report, :general_report)
    end
end

我的 GeneralReport 模型:

class GeneralReport < ActiveRecord::Base

  # Adds Model Relationships

    ## GeneralReport -> GenRepEnt
    has_many :gen_rep_ents, dependent: :destroy
end

我的 GenRepEnt 模型:

class GenRepEnt < ActiveRecord::Base

  belongs_to :general_report
  
end

这是 Routes.rb 文件:

Rails.application.routes.draw do

  resources :general_reports do 
    resources :gen_rep_ents, except: [:index], controller: 'general_reports/gen_rep_ents'
  end
  
end

这里是嵌套路由的 Rake/Routes

  general_report_gen_rep_ents POST   /general_reports/:general_report_id/gen_rep_ents(.:format)          general_reports/gen_rep_ents#create
 new_general_report_gen_rep_ent GET    /general_reports/:general_report_id/gen_rep_ents/new(.:format)      general_reports/gen_rep_ents#new
edit_general_report_gen_rep_ent GET    /general_reports/:general_report_id/gen_rep_ents/:id/edit(.:format) general_reports/gen_rep_ents#edit
     general_report_gen_rep_ent GET    /general_reports/:general_report_id/gen_rep_ents/:id(.:format)      general_reports/gen_rep_ents#show
                                PATCH  /general_reports/:general_report_id/gen_rep_ents/:id(.:format)      general_reports/gen_rep_ents#update
                                PUT    /general_reports/:general_report_id/gen_rep_ents/:id(.:format)      general_reports/gen_rep_ents#update
                                DELETE /general_reports/:general_report_id/gen_rep_ents/:id(.:format)      general_reports/gen_rep_ents#destroy

如果这是一个简单的问题,我深表歉意,但可能只是我需要重新审视它,因为我已经投入了很长时间。非常感谢所有帮助!如果需要更多信息,请告诉我。

编辑 # 2

提供的服务器日志:

Started GET "/general_reports/%23%3CGenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30%3E/gen_rep_ents/new" for ::1 at 2015-09-10 02:01:04 -0600
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by GeneralReports::GenRepEntsController#new as HTML
  Parameters: {"general_report_id"=>"#<GenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30>"}
  GeneralReport Load (0.3ms)  SELECT  "general_reports".* FROM "general_reports" WHERE "general_reports"."id" =  LIMIT 1  [["id", 0]]
Completed 404 Not Found in 22ms (ActiveRecord: 2.9ms)

ActiveRecord::RecordNotFound (Couldn't find GeneralReport with 'id'=#<GenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30>):
  app/controllers/general_reports/gen_rep_ents_controller.rb:17:in `new'


  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (10.4ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.5ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (67.1ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.8ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.8ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.7ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.8ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (46.0ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.6ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.3ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (108.5ms)


Started GET "/general_reports/%23%3CGenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30%3E/gen_rep_ents/new" for ::1 at 2015-09-10 02:01:04 -0600
Processing by GeneralReports::GenRepEntsController#new as HTML
  Parameters: {"general_report_id"=>"#<GenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30>"}
  GeneralReport Load (0.2ms)  SELECT  "general_reports".* FROM "general_reports" WHERE "general_reports"."id" =  LIMIT 1  [["id", 0]]
Completed 404 Not Found in 2ms (ActiveRecord: 0.2ms)

ActiveRecord::RecordNotFound (Couldn't find GeneralReport with 'id'=#<GenRepEnt::ActiveRecord_Associations_CollectionProxy:0x007fa547608f30>):
  app/controllers/general_reports/gen_rep_ents_controller.rb:17:in `new'


  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.0ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.4ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (54.8ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.5ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.6ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.7ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (43.2ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.7ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.9ms)
  Rendered /Users/TaurenLTD1/.rvm/gems/ruby-2.2.0/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (96.0ms)

新方法需要找到母总报表。所以link路径需要包含父对象的id。

所以不是这个:

 <%= link_to "New Report Entry", new_general_report_gen_rep_ent_path(@gen_rep_ents) %>

你需要这个:

 <%= link_to "New Report Entry", new_general_report_gen_rep_ent_path(@general_report) %>