Accessing first row in an object

sir_thursday
<% question = @questions.try(:first) %>
<h1><%= question.title %></h1>

I'm trying to use the above code to grab the first question from a Questions model, and then display it's title in HTML. However, I'm throwing the error undefined method 'title' for nil:NilClass

Autogenerated RoR files use the question.title line... why isn't it working here?

Kirti Thorat

question.title raises error as undefined method 'title' for nil:NilClass that means question is set as nil. You set question using

<% question = @questions.try(:first) %>

It means either @questions is nil or there @questions.first returns nil.

Make sure that you set @questions instance variable in the Controller's action which renders this particular view.

 def action_name
   @questions = Question.all
 end

Also, if you just want to show only the first question record in your view and you are not going to use @questions anywhere then just set

 def action_name
   @question = Question.first
 end 

and use it in the view directly as:

<h1><%= @question.try(:title) %></h1>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

HttpContext is null when accessing injected object with Ninject on first request through async/await

分類Dev

Complexity of accessing data in an object

分類Dev

Accessing an Object from Within an Object

分類Dev

Accessing JSON object in Handlebars [object Object] error

分類Dev

Accessing a property of an object inside an object inside an object

分類Dev

Difficulty accessing window object in Cypress

分類Dev

Accessing object for every agent in JADE

分類Dev

Accessing part of an object using path

分類Dev

Accessing a json object gives undefined

分類Dev

Accessing fields with '@' symbol of object in dGrid

分類Dev

Accessing variable from JavaScript object

分類Dev

Accessing object properties in another function

分類Dev

NullPointerException by accessing Object in JButton Array

分類Dev

Accessing Properties of one Object from another Object

分類Dev

Accessing the properties of an object of an object dynamically in PHP

分類Dev

Return first matching row

分類Dev

accessing row record by id error in rails?

分類Dev

Accessing a range of the first element of a tuple in a list of tuples

分類Dev

Scrollable table with first column and first row pinned

分類Dev

How to get the first row that appears first in the data?

分類Dev

Accessing a HashMap value with a custom Pair Object as a key

分類Dev

Android Realm - Accessing Realm Object from Service

分類Dev

node js mysql accessing javascript object

分類Dev

Accessing a common data object in a PyQt application

分類Dev

Accessing a pages object using a Chrome Extension

分類Dev

Accessing second array (non object) in decoded JSON

分類Dev

PHP accessing protected key in object returns empty

分類Dev

Accessing static method using null object

分類Dev

Javascript: Accessing multi-dimensional object properties

Related 関連記事

  1. 1

    HttpContext is null when accessing injected object with Ninject on first request through async/await

  2. 2

    Complexity of accessing data in an object

  3. 3

    Accessing an Object from Within an Object

  4. 4

    Accessing JSON object in Handlebars [object Object] error

  5. 5

    Accessing a property of an object inside an object inside an object

  6. 6

    Difficulty accessing window object in Cypress

  7. 7

    Accessing object for every agent in JADE

  8. 8

    Accessing part of an object using path

  9. 9

    Accessing a json object gives undefined

  10. 10

    Accessing fields with '@' symbol of object in dGrid

  11. 11

    Accessing variable from JavaScript object

  12. 12

    Accessing object properties in another function

  13. 13

    NullPointerException by accessing Object in JButton Array

  14. 14

    Accessing Properties of one Object from another Object

  15. 15

    Accessing the properties of an object of an object dynamically in PHP

  16. 16

    Return first matching row

  17. 17

    accessing row record by id error in rails?

  18. 18

    Accessing a range of the first element of a tuple in a list of tuples

  19. 19

    Scrollable table with first column and first row pinned

  20. 20

    How to get the first row that appears first in the data?

  21. 21

    Accessing a HashMap value with a custom Pair Object as a key

  22. 22

    Android Realm - Accessing Realm Object from Service

  23. 23

    node js mysql accessing javascript object

  24. 24

    Accessing a common data object in a PyQt application

  25. 25

    Accessing a pages object using a Chrome Extension

  26. 26

    Accessing second array (non object) in decoded JSON

  27. 27

    PHP accessing protected key in object returns empty

  28. 28

    Accessing static method using null object

  29. 29

    Javascript: Accessing multi-dimensional object properties

ホットタグ

アーカイブ