用于NilClass的未定义方法`model_name':Class - rails4

时间:2015-08-12 00:44:05

标签: ruby-on-rails ruby ruby-on-rails-4

  

我完全不确定为什么我会收到这个错误 - 我相信我有   在我的报表控制器中正确定义了我的变量 - 它没有   让我明白为什么我收到错误。任何帮助   理解这一点将非常感激。

我目前收到错误消息

undefined method `model_name' for NilClass:Class

enter image description here

控制台

  Rendered reports/_form.html.erb (4.6ms)
  Rendered shared/_content_advert_show.html.erb (7.8ms)
  Rendered adverts/show.html.erb within layouts/application (10.3ms)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.5ms)

NoMethodError - undefined method `model_name' for NilClass:Class:
  actionpack (4.1.10) lib/action_controller/model_naming.rb:9:in `model_name_from_record_or_class'
  actionview (4.1.10) lib/action_view/record_identifier.rb:47:in `dom_class'
  simple_form (3.0.2) lib/simple_form/action_view_extensions/form_helper.rb:58:in `simple_form_css_class'
  simple_form (3.0.2) lib/simple_form/action_view_extensions/form_helper.rb:19:in `simple_form_for'
  app/views/reports/_form.html.erb:1:in `_app_views_reports__form_html_erb__795468471443709165_70250827830400'
  actionview (4.1.10) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.1.10) lib/active_support/notifications.rb:161:in `instrument'

模式

  create_table "reports", force: true do |t|
    t.text     "content"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "category_report_id"
  end

  create_table "category_reports", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

report.rb

class Report < ActiveRecord::Base
  belongs_to :category_report
  has_many :adverts
end

catergory_report.rb

class CategoryReport < ActiveRecord::Base
  has_many :reports
  has_many :adverts
end

advert.rb

class Advert < ActiveRecord::Base
  belongs_to :report
  belongs_to :category_report
end

reports / _form.html.erb

<%= simple_form_for(@report) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.association :category_report, collection: CategoryReport.all, prompt: "please choose", required: true, label: 'report this advert' %>
    <%= f.input :content %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "send report" %>
  </div>
<% end %>

reports_controller.rb

class ReportsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :set_report, only: [:show, :edit, :update, :destroy]

  def index
    @reports = Report.all
    respond_with(@reports)
  end

  def show
    respond_with(@report)
  end

  def new
    @report = Report.new
    respond_with(@report)
  end

  def edit
  end

  def create
    @report = Report.new(report_params)
    @report.save
    respond_with(@report)
  end

  def update
    @report.update(report_params)
    respond_with(@report)
  end

  def destroy
    @report.destroy
    respond_with(@report)
  end

  private
    def set_report
      @report = Report.find(params[:id])
    end

    def report_params
      params.require(:report).permit(:content, :category_report_id)
    end
end

seed.rb

CategoryReport.delete_all
crt1 = CategoryReport.create!(name:'spam')
crt2 = CategoryReport.create!(name:'disturbing content')
crt3 = CategoryReport.create!(name:'incorrect information')

1 个答案:

答案 0 :(得分:0)

before_action :set_report, only: [:show, :edit, :update, :destroy]位于ReportsController。导致错误的控制器是AdvertsController。您还需要在该控制器中设置报告。

如果您在很多地方都有此功能,可以考虑将方法移至ApplicationController

除了@report和之前的过滤器,您还可以考虑以下内容:

helper_method: :report

def report
  @_report ||= params[:id] ? Report.find(params[:id]) : Report.new
end