I have made significant progress in Rails since my last post. So here is what the goal is!
- Created "Course" objects from raked csv file.
- Access the attributes of those courses in order to print them onto a table.
The rake executes without any errors, so first QUESTION. How can I check to see if the objects were actually created?
Rake File:
require 'csv'
namespace :data do
desc "Imports data from course_listing.csv"
task :import_csv => :environment do
CSV.foreach("db/course_listing.csv") do |row|
Course.new(
:ID => row[0],
:type => row[1],
:location => row[2],
:meeting_days => row[3],
:start_time => row[4],
:stop_time => row[5],
)
end
end
end
Next, here are my controller and model. All I need to do is print the attributes so the model should be barebones.
Controller:
class CoursesController < ApplicationController
def new
@courses = Course.new
end
def create
@course = Course.create(:ID, :type, :location, :meeting_days, :start_time, :stop_time)
end
def index
@courses = Course.all
end
end
Model:
class Course < ActiveRecord::Base
attr_accessor :ID, :type, :location, :meeting_days, :start_time, :stop_time
end
Lastly here is the view thats throws the exception "undefined method `each' for #Course id: nil, created_at: nil, updated_at: nil"
View:
<table border="1" style="width:50%">
<tr>
<th>Type</th>
<th>Location</th>
<th>Meeting Days</th>
<th>Start Time</th>
<th>Stop Time</th>
</tr>
<% @courses.each do |course| %>
<tr>
<td><%= course.type %></td>
<td><%= course.location %></td>
<td><%= course.meeting_days %></td>
<td><%= course.start_time %></td>
<td><%= course.stop_time %></td>
</tr>
<% end %>
</table>
So how can I fix this :) I am new to rails and am still getting used to it!
Thanks!