Rails - 路由 - 奇怪的错误

时间:2015-09-29 10:27:27

标签: ruby-on-rails

我正在制作rails 4应用程序。

我刚刚为大学创建了一个脚手架。

university表有三个属性,分别是:name,:logo,:post_code。

我创建了三所大学作为考试。他们进入控制台。

university.rb模型具有以下关联: 大学模式有这些联系:

has_many :faculties
  has_many :students
  has_many :academics#, through: :researchers
  has_many :educators
  has_many :faculties
  has_many :courses, through: :faculties
  has_many :programs#, through: :academics
  has_many :alumnus

  has_one :policy_ip
  has_one :policy_publication
  has_one :policy_commerciality

  has_many :eaip_assets
  has_many :commercial_ip_assets
  has_many :community_activities
  has_many :ip_transfer_successes
  has_many :spin_outs
  has_many :ip_asset_managers#, through: :universities
  has_many :expression_of_interest_options#, through: :ip_asset_manager,  -> { where submitted: true }
  has_many :expression_of_interest_assignments#, through: :ip_asset_manager,  -> { where submitted: true }
  has_many :expression_of_interest_licensings#, through: :ip_asset_manager,  -> { where submitted: true }
  has_many :expression_of_interest_collaborations#, through: :ip_asset_manager,  -> { where submitted: true }
  has_many :expression_of_interest_spin_outs#, through: :ip_asset_manager,  -> { where submitted: true }



  has_many :awards
  has_many :profiles

查看大学#show有:

  <div class="col-md-7 col-md-offset-1">
    <div class="profilet"><%= @university.name %></div>
<%= link_to 'Edit', edit_university_path(@university) %> |
<%= link_to 'Back', universities_path %>

我得到的第一个错误(在我的3条记录中):

undefined method `name' for nil:NilClass

当我更改线条以使其打开时:

    <div class="profilet"><%= @university.try(:name) %></div>

我在“修改”链接上收到错误消息:

No route matches {:action=>"edit", :controller=>"universities", :id=>nil} missing required keys: [:id]

当我删除编辑链接并再试一次时,我会看到一个带有“后退”链接的空白页面。

我无法理解什么是错的。我的控制台显示我在大学表中肯定有3条记录(每条记录的ID为1,2或3)。我的路线是资源 - (与我所有其他模型一样)这是我在编辑链接中遇到的第一个问题。

在我的大学控制器中,我对表中的三个属性中的每一个都有强大的参数,所以我仍然坚持要尝试解决这个问题。

有什么想法吗?

大学管理者有:

class UniversitiesController < ApplicationController
  # before_action :set_university, only: [:show, :edit, :update, :destroy]

  # GET /universities
  # GET /universities.json
  def index
    @universities = University.all
  end

  # GET /universities/1
  # GET /universities/1.json
  def show

  end

  # GET /universities/new
  def new
    @university = University.new
  end

  # GET /universities/1/edit
  def edit
  end

  # POST /universities
  # POST /universities.json
  def create
    @university = University.new(university_params)

    respond_to do |format|
      if @university.save
        format.html { redirect_to @university, notice: 'University was successfully created.' }
        format.json { render action: 'show', status: :created, location: @university }
      else
        format.html { render action: 'new' }
        format.json { render json: @university.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /universities/1
  # PATCH/PUT /universities/1.json
  def update
    respond_to do |format|
      if @university.update(university_params)
        format.html { redirect_to @university, notice: 'University was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @university.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /universities/1
  # DELETE /universities/1.json
  def destroy
    @university.destroy
    respond_to do |format|
      format.html { redirect_to universities_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def university_params
      params[:university].permit(:name, :logo, :post_code)
    end
end

4 个答案:

答案 0 :(得分:0)

关联的实例变量@universitynil,因此路由助手无法为其生成路由。

请检查您的控制器的edit操作并解决问题。

答案 1 :(得分:0)

你的@university的id = nil,如果你想使用edit url helper,你需要提供带有id的@university实体。换句话说,您可以编辑DB中存在的记录,否则您应该创建新的。

答案 2 :(得分:0)

哇,你这里有很多问题。

  

nil的未定义方法`name':NilClass

这意味着您的一个变量未初始化。 原因就是乐趣的开始。

定义<%= @university.x %>时,您在@university变量上调用方法。如果该变量未填充任何数据,或者未定义,则Ruby会自动为其分配NilClass类。

您提到在引用@university变量时看到此错误。这意味着在路由控制器中,它没有被填充。

以下是修复方法:

#config/routes.rb
resources :universities #-> url.com/universities/:id
#app/controllers/universities_controller.rb
class UniversitiesController < ApplicationController
   before_action :set_university, only: [:show, :edit, :update, :destroy]

   def index
      @universities = University.all
   end

   def edit
   end

   def show
   end
end

这应该使您能够使用以下内容:

#app/views/universities/show.html.erb
<% if @university %>
   <%= @university.name %>
<% end %>

答案 3 :(得分:0)

这意味着你在行动中没有对象大学。您必须启用以下行:

before_action :set_university, only: [:show, :edit, :update, :destroy]
相关问题