Hi guys, I just started using Ruby on Rails. After implementing the RoR blog tutorial I started with my own data model. Sadly I am not able to get my create page running.
Model: class Activity < ActiveRecord::Base validates :activity, :presence => true validates :forKids, :presence => true validates :start_date, :presence => true end
Controller: class ActivitiesController < ApplicationController def index @activities = Activity.all
respond_to do |format| format.html # index.html.erb format.json { render json: @activities } end end end
def new @activity = Activity.new
respond_to do |format| format.html # new.html.erb format.json { render json: @activity } end end
View: <h1>New Activity</h1>
<%= render 'form' %>
<%= link_to 'Back', activities_path %>
and partial: <%= form_for(@activity) do |f| %> <% if @activity.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
<ul> <% @activity.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
<div class="field"> <%= f.label :activity %><br /> <%= f.text_field :activity %> </div> <div class="field"> <%= f.label :responsible %><br /> <%= f.text_field :responsible %> </div>
I always get the following error message:
NoMethodError in Activities#new
Showing .../app/views/activities/_form.html.erb where line #1 raised: undefined method `model_name' for NilClass:Class
Extracted source (around line #1): 1: <%= form_for(@activity) do |f| %> 2: <% if @activity.errors.any? %> 3: <div id="error_explanation"> 4: <h2><%= pluralize(@activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
I found several posts about the same error message but so far I wasn't able to figure out the problem with @activity being nil. Shouldn't it be initialized by @activity = Activity.new in the controller?
Thanks Julian