2016-06-28 18 views
0

「アプリケーション」の命名規則が不適切であることに注意してください。これは奨学金申請ポータルであるためです。Rails:NoMethodError in Applications#index

アプリケーションのインデックスを開こうとすると、次のエラーが発生します。

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> 

ここでは、そのページのコードは次のようになります。

<% 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 %> 

ユーザーモデル:

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 

能力モデル:

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 

アプリケーションモデル:

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

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 
+0

への変更has_one :applicationsは、あなたの 'User'モデルを投稿することができますか? – treiff

+0

あなたのuser.rbコードを投稿してもよろしいですか?アプリケーションの関係を定義していないか、その名前のメソッドがユーザーのモデルに存在しないようです。 – fanta

+0

モデルで更新しました! –

答えて

0

私はあなたがapplication.rbと呼ばれるモデルを作ったと仮定するつもりです。 current_user.applicationと入力すると、Railsはapplicationというメソッドを検索します。これは、モデルでデータベース関連を指定して、アプリケーションと呼ばれるテーブルに接続する場合にのみ機能します。私は関連が何であるか分かりませんが、あなたが.application(単数)を呼び出す場合は、ユーザモデルにhas_one :applicationまたはbelongs_to :applicationのいずれかを追加する必要があると思います。また、対応するデータベース関連付けをアプリケーションモデルに追加する必要があることに注意してください。また、Railsはアプリケーションという言葉が好きではないことがあります。

また、名前を付けた属性は、アプリケーションではなくユーザーに属すべき属性と非常によく似ています。 .applicationがまったく必要ない可能性はありますか?これはユーザーモデルの属性ですか?それはcurrent_user.application.emailではなく、current_user.emailのようなコードを書くことを意味します。

+0

私はちょうどそれらのモデルのコードでそれを更新しました!私が意味したのは、current_userのアプリケーションを投稿し、そのアプリケーションからの電子メール(電子メールはアプリケーションで入力するフィールド)です。 –

0

今、あなたはより多くのコードが含まれていたことの答えは非常に明確である:has_one :application

+0

それで、私はそれをして、別のエラーを返しました –

+0

何のエラーでしたか君は? – Philip7899

+0

これが答えです。別のエラーが発生しているという事実は、これが正確であることを意味します。あなたが新しいエラーで質問を更新したように思えました。あなたの新しい問題に対する答えはここにあります。あなたのユーザの中にはアプリケーションがない人がいるので、current_user.applicationを呼び出すとnilを返し、 .nameをnil – Philip7899

関連する問題