Search code examples
ruby-on-railsrubydeviseassociationsmodels

Associating two models together in Rails 4+


I have a User model which is working under Devise with no problems (using devise sanitizer to update fields, so no UsersController) I am working on creating a Quiz model, which belongs_to the User model, and the User model has_one Quiz. In my routes, I have: resources :users, :quizzes (is this supposed to be quizzes or quizs? I know that Rails pluralizes but couldn't seem to find which it'd be in this case).

In my views, I'm trying to open up a modal (which works) and inside have it populated with fields that a User can enter in questions they want (q1 through q5 being the database fields). Inside the modal content area, I have the code:

<%= form_for @quiz, url: {action: "new"} do |f| %>
<%= f.submit "Create" %>
<% end %>

and I get the error "First argument in form cannot contain nil or be empty"

Inside my quizzes controller, I have defined new as

def new
@quiz = Quiz.new
end

I would greatly appreciate some assistance here! Thank you.


Solution

  • In your WelcomeController action: index add this line to initialized @quiz

    def index 
      @quiz = Quiz.new
    end
    

    hope you made a good progress in your project.