Forum: Ruby on Rails validates_presence_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fcf5e28e2e7ce245632a4a29896e6799?d=identicon&s=25 ss (Guest)
on 2006-04-19 14:26
Hi, having a few problems working out whats kicking off,

im using validates_presence_of :title in a models class

this seemed to work fine untill I used a custom new.rhtml

error message I am gaining is

 NoMethodError in Recipe#create

Showing app/views/recipe/new.rhtml where line #15 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each

12:  <p>
13:  <b>Category:</b><br/>
14:  <select name="recipe[category_id]">
15:  <% @categories.each do |category| %>
16:  <option value="<%= category.id %>">
17:  <%= category.name %>
18:  </option>

the def create controller looks somthing like this

        def create
		@recipe = Recipe.new(@params['recipe'])
		@recipe.date = Date.today
		if @recipe.save
			redirect_to :action => 'list'
			else
			render_action 'new'
		end
	end

any suggestions to where im going wrong?
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-19 14:36
(Received via mailing list)
It looks like you need to get @categories in your create method so that
it will be available in your template.
This topic is locked and can not be replied to.