0

When I try to access the view, an error is returned:
undefined method 'title' for #Task id: nil, created_at: nil, updated_at: nil

tasks_controller.rb (Controller)

class TasksController < ApplicationController
def new
    @task = Task.new
end


    def create
        @task = Task.new(params[:task])
        if @task.save
        redirect_to new_task_path
        end
    end 
end

/tasks/new.html.erb (View)

<%= form_for :task, url: tasks_path do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :details %><br>
    <%= f.text_area :details %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>

task.rb (Model)

class Task < ActiveRecord::Base
    belongs_to :user
    attr_accessible :title, :details, :user_id, :volunteers
end

What should I do?

2 Answers 2

1

You have not defined fields in your database, see:

#Task id: nil, created_at: nil, updated_at: nil

There is no title nor details there, do this:

rails g migration add_title_and_details_to_tasks title details

Check that your migration file is correctly creating these 2 fields.

Then run rake db:migrate. Next time remember to generate your resource with these fields:

rails g scaffold Task title details

This way, when you migrate your fields will be there.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you have pending migration(s) (do you have title in your schema.rb).

other note: build your form for @task

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.