首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails: Applications#index中的NoMethodError

Rails: Applications#index中的NoMethodError
EN

Stack Overflow用户
提问于 2016-06-29 01:43:47
回答 2查看 79关注 0票数 0

请注意,“应用程序”是一个糟糕的命名约定。这是因为它是一个奖学金申请门户。

当我试图打开应用程序的索引时,我得到了以下错误。

代码语言:javascript
复制
NoMethodError in Applications#index

Showing /home/zane/scholarship-application/app/views/applications/_current_user_application.html.erb where line #22 raised:

undefined method `name' for nil:NilClass

Extracted source (around line #22):

20
21
22
23
24
25




  <tbody>
      <tr>
        <td><%= current_user.application.name %></td>
        <td><%= current_user.application.name %></td>
        <td><%= current_user.application.gender %></td>
        <td><%= current_user.application.date_of_birth %></td>

下面是该页面的代码:

代码语言:javascript
复制
<% if user_signed_in? && current_user.role == "User" %>

<h1 class="center">Your Application</h1>

<table class="table">
  <thead>
    <tr>
      <th>Name</th>
      <th>Gender</th>
      <th>Date of birth</th>
      <th>GPA</th>
      <th>Address</th>
      <th>State</th>
      <th>University</th>
      <th>Essay</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
      <tr>
        <td><%= current_user.application.name %></td>
        <td><%= current_user.application.gender %></td>
        <td><%= current_user.application.date_of_birth %></td>
        <td><%= current_user.application.gpa %></td>
        <td><%= current_user.application.address %></td>
        <td><%= current_user.application.state %></td>
        <td><%= current_user.application.university %></td>
        <td><%= current_user.application.essay %></td>
        <td><%= link_to 'Show', current_user.application %></td>
        <td><%= link_to 'Edit', current_user.edit_application_path(application) %></td>
        <td><%= link_to 'Destroy', current_user.application, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
  </tbody>
</table>

<% end %>

用户模型:

代码语言:javascript
复制
class User < ActiveRecord::Base
    #Defining different roles
    enum role: [:Guest, :User, :Admin]
    #Users can only have one scholarship application
    has_one :applications
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

能力模型:

代码语言:javascript
复制
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
        if user.role == "Admin"
            can :manage, :all
        elsif user.role == "User"
            can :manage, Application
            can :manage, User
        else
            can :read, Static_Page
        end
    end
end

应用模型:

代码语言:javascript
复制
class Application < ActiveRecord::Base
    belongs_to :user

    #Users may only submit one application
    validate :limit_applications, :on => :create

    #User must fully fill out all forms application
    validates :name, presence: true
    validates :gender, presence: true
    validates :date_of_birth, presence: true
    validates :gpa, presence: true
    validates :university, presence: true
    validates :address, presence: true
    validates :state, presence: true

    private
    def limit_applications
        limit = 1
        if self.user.applications.(:reload).count >= limit
            errors.add(:base, "You can only create #{limit} application.")
            end
        end
 end

Schema.rb

代码语言:javascript
复制
create_table "applications", force: :cascade do |t|
    t.string   "name"
    t.string   "gender"
    t.date     "date_of_birth"
    t.string   "gpa"
    t.text     "essay"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.string   "university"
    t.string   "address"
    t.string   "state"
    t.integer  "user_id"
  end

  create_table "entries", force: :cascade do |t|
    t.string   "name"
    t.boolean  "winner"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "nasa_apis", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.string   "email",                  default: "",    null: false
    t.string   "encrypted_password",     default: "",    null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.integer  "role",                   default: 1
    t.boolean  "winner",                 default: false
    t.integer  "application_id"
  end
EN

回答 2

Stack Overflow用户

发布于 2016-06-29 03:03:01

我假设你做了一个名为application.rb的模型。当您键入current_user.application时,Rails会搜索一个名为application的方法。只有当您在模型中指定数据库关联以将其连接到名为applications的表时,这才会起作用。我不知道这是什么关联,但是如果你调用.application (单数),我想你需要在你的用户模型中添加一个:has_one :applicationbelongs_to :application。请记住,您还需要将相应的数据库关联添加到应用程序模型。Rails也有可能不喜欢应用程序这个词,这可能会把一切都搞砸。

此外,您命名的属性听起来非常类似于应该属于用户而不是应用程序的属性。有没有可能你根本不需要.application,这些都是你的用户模型的属性?这意味着要编写像current_user.email这样的代码,而不是current_user.application.email

票数 0
EN

Stack Overflow用户

发布于 2016-06-30 03:33:52

现在您已经包含了更多的代码,答案就很清楚了:将has_one :applications更改为has_one :application

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38082919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档