Hi, I tried out the ToDo List tutorial, and curious to explore further
to learn more abt Rails.
a) I added the following validation rules in the Todo model:
class Todo < ActiveRecord::Base
validates_presence_of :description
b) In my Todo controller, I modified the ''add_item'' action:
def list
@not_done = Todo.find_not_done
@done = Todo.find_done
end
def add_item
@item = Todo.new
@item.attributes = @params["new_item"]
if @item.save
redirect_to(:action => "list")
else
render_action "list" # <-- modified
end
end
c) In my view, I added the following:
<% unless @item == nil or @item.errors.empty? %>
The item couldn''t be saved due to these errors:
<ul>
<% @item.errors.each_full do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
Now by adding an empty description, the validation rule takes effect
and display the error message "Description can''t be empty".
However,
the @not_done and @done objects now become ''nil'' in my view.
Why is
this so? I assume the ''list'' method will be called when
render_action
is invoked.
Any tips or advice is much appreciated!:)