Rails undefined方法`model_name'为零:NilClass

时间:2016-09-01 21:51:11

标签: ruby-on-rails ruby

我有一个小Rails站点,有2个控制器讲师请求。站点管理员只能访问讲师以管理和管理它们(所有方法都使用设备进行身份验证)。第二个控制器讲师是访问访问站点以查看列出的教师并为他们选择的教师创建请求的方式。我在创建新请求时遇到问题,这些新请求应该保存名称手机电子邮件 消息作为从请求显示页面中选择的 instructor_id 。我希望我的描述清楚。

感谢。

请求控制器

class RequestsController < ApplicationController
  def index
    if params[:search].present?
      @instructors = Instructor.near(params[:search], 50)
    else
      # Shows all listed instructors by the created date.
      @instructors = Instructor.order('created_at DESC')
    end
  end

  def show
    @instructor = Instructor.find(params[:id])
  end

  def new
    @request = Request.new
  end

  def create
    @request = Request.new(request_params)
    #@request = Request.new(request_params)
    if @request.save
      flash[:success] = "Request has been submited!"
    else
      flash[:alert] = "Something is went wrong!"
    end
  end

  private

  def request_params
    params.require(:request).permit(:name, :email, :phone, :message)
  end
end

请求显示

<header><h1 class="display-4"><%= @instructor.name %></h1></header>

      <h3>Address: <%= @instructor.address %></h3>
      <h3>Car Model: <%= @instructor.car %></h3>
      <hr>
      <%= simple_form_for(@instructor, :url =>{ :action => "create" }) do |f| %>
        <%= f.input :name, label: "Your name" %>
        <%= f.input :email %>
        <%= f.input :phone, label: "Phone number" %>
        <%= f.input :message, as: :text %>
        <br>
        <%= f.button :submit, class: "btn btn-danger" %>
      <% end %>
      <br>
      <br>

教练控制器

class InstructorsController < ApplicationController
      # Admin is required to login to access any action on this controller
      before_action :authenticate_admin! # except: [:action, :action] to unlock any action

      def index
        @instructors = Instructor.order('created_at DESC')
      end

      def show
        find_instructor
      end

      def new
        @instructor = Instructor.new
      end

      def update
        find_instructor
        if @instructor.update(instructor_params)
          redirect_to @instructor
          flash[:notice] = "Instructor changes has been saved."
        else
          render 'edit'
        end
      end

      def create
        @instructor = Instructor.new(instructor_params)
        if @instructor.save
          flash[:notice] = "Instructor has been saved!"
          redirect_to instructors_path
        else
          render 'new'
          flash_error('New instructor hasn\'t been saved.')
        end
      end

      def edit
        find_instructor
      end

      def destroy
        find_instructor
         if @instructor.destroy
           redirect_to root_path
           flash[:notice] = "The instructor has been deleted."
         else
           flash_error('Instructor hasn\'t been deleted.')
         end
      end

        private

        def flash_error(message) # Takes the of the error message as an argument
          flash[:alert] = "Something went wrong!, #{message}
                           Please make sure you submitting valid data and try again"
        end

        def find_instructor
          @instructor = Instructor.find(params[:id])
        end

        def instructor_params
           params.require(:instructor).permit(:name, :car, :address, :manual, :auto)
        end
    end
           flash_error('Instructor hasn\'t been deleted.')
         end
      end

        private

        def flash_error(message) # Takes the of the error message as an argument
          flash[:alert] = "Something went wrong!, #{message}
                           Please make sure you submitting valid data and try again"
        end

        def find_instructor
          @instructor = Instructor.find(params[:id])
        end

        def instructor_params
           params.require(:instructor).permit(:name, :car, :address, :manual, :auto)
        end
    end

讲师模型

class Instructor < ApplicationRecord
  has_many :requests
  geocoded_by :address
  after_validation :geocode, if: ->(obj){ obj.address.present? and obj.address_changed? }
end

请求模型

class Request < ApplicationRecord
  belongs_to :instructor
end

1 个答案:

答案 0 :(得分:-2)

我认为错误来自展示视图中的simple_form_for,因为@instructor为零。

你得到的变量是:

@instructor = Instructor.find(params[:id])

你应该做一个测试,确保存在一个带有params发送的id的教师。