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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 April 2012 13:17, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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?I think the problem is that you have just used <%= render ''form'' %> to render the form. You have not passed the @activity variable to the form. Have a look at the Rails Guide on Layouts and Rendering for how to use :locals to pass variables when rendering a partial. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #1056490:> > I think the problem is that you have just used > <%= render ''form'' %> > to render the form. You have not passed the @activity variable to the > form. Have a look at the Rails Guide on Layouts and Rendering for how > to use :locals to pass variables when rendering a partial. > > ColinThanks for your response Colin. I tried to test it without the partial so I replaced the <%= render ''form'' %> with the body of the partial but I still get the same error message. So the passing of the variable into the partial cannot be the reason for the problem. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 April 2012 14:11, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1056490: >> >> I think the problem is that you have just used >> <%= render ''form'' %> >> to render the form. You have not passed the @activity variable to the >> form. Have a look at the Rails Guide on Layouts and Rendering for how >> to use :locals to pass variables when rendering a partial. >> >> Colin > > Thanks for your response Colin. > I tried to test it without the partial so I replaced the <%= render > ''form'' %> with the body of the partial but I still get the same error > message. > So the passing of the variable into the partial cannot be the reason for > the problem.Put the form back in the view if you really think that and try again. If you still get the error then post the view and the error message. Copy and paste, do not retype. Colin> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
new.html.erb
<h1>New Activity</h1>
<%= 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>
<div class="field">
<%= f.label :mail %><br />
<%= f.text_field :mail %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :link %><br />
<%= f.text_field :link %>
</div>
<div class="field">
<%= f.label :forKids %><br />
<%= f.text_field :forKids %>
</div>
<div class="field">
<%= f.label :start_date %><br />
<%= f.text_field :start_date %>
</div>
<div class="field">
<%= f.label :end_date %><br />
<%= f.text_field :end_date %>
</div>
<div class="field">
<%= f.label :iteration %><br />
<%= f.text_field :iteration %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
NoMethodError in Activities#new
Showing .../app/views/activities/new.html.erb where line #3 raised:
undefined method `model_name'' for NilClass:Class
Extracted source (around line #3):
1: <h1>New Activity</h1>
2:
3: <%= form_for @activity do |f| %>
4: <% if @activity.errors.any? %>
5: <div id="error_explanation">
6: <h2><%= pluralize(@activity.errors.count,
"error") %>
prohibited this activity from being saved:</h2>
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 April 2012 13:17, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > endWhat does the last end match with? Colin> > 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 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote in post #1056501:> On 14 April 2012 13:17, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> end >> end >> end > > What does the last end match with? > > Colin >Damn it that was the mistake. I was always looking at the wrong spot. Thank you very much Colin. Julian -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 14 April 2012 15:55, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote in post #1056501: >> On 14 April 2012 13:17, Julian P. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> end >>> end >>> end >> >> What does the last end match with? >> >> Colin >> > > Damn it that was the mistake. I was always looking at the wrong spot. > Thank you very much Colin.I don''t understand why that did not give an error, the controller#new method should not have been found. For future similar problems have a look at the Rails Guide on Debugging. It will show you how to debug the code so that these problems can be more easily found. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.