jenkhai
jenkhai

Reputation: 59

Form Helper Undefined Method

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?

Upvotes: 0

Views: 684

Answers (2)

sites
sites

Reputation: 21785

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.

Upvotes: 1

gotva
gotva

Reputation: 5998

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

other note: build your form for @task

Upvotes: 1

Related Questions