helpisgood helpisgood - 1 year ago 62
Ruby Question

Object is all nil in Rails 4+?

I have a 'landing.html.erb' under a folder 'Welcome' in my Views. In the WelcomeController, I have the following:

def landing
@quiz =

to create a new Quiz. When the User clicks the create button,

def create
@quiz =
flash[:success] = "Updated"
redirect_to @quiz
render 'new'

def quiz_params

is the code I have for the QuizzesController. However, this gives the error:

undefined method `build' for nil:NilClass

specifically at the line

@quiz =

I ran the rails console and typed and it displayed every field as 'nil' and I'm not entirely sure what I need to modify and would appreciate guidance.

The Quiz model belongs_to User.
The User has_one Quiz.

Thank you very much.

Answer Source

Here I am assuming you are having has_one association among user and quiz. So for build the associated object you need to use the following code.


This will build the quiz object with the user_id field contains the id of the current user and other fields according to quiz_params.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download