Hello Abdullah,
1. You need to add the name of the action after todo/, so to see the
list, you would need to go to http://localhost:3000/todo/list. If you
don''t specify an action, Rails will look for an action called index.
If you want list to be your default action, you could add this to
app/controllers/todo_controller.rb:
def index
redirect_to :action => "list"
end
2. That''s an issue with SQLite I think. Try modifying add_items to
this:
def add_item
item = Todo.new # Create a new instance of Todo, so create a new item
item.attributes = @params["new_item"] # The fields of item should
be set to what''s in the "new_item" hash
item.done = 0
if item.save # Try to save our item into the database
redirect_to(:action => "list") # Return to the list page if it
suceeds
else
render_text "Couldn''t add new item" # Print an error
message otherwise
end
end
Good luck,
Vincent.
On Sat, 20 Nov 2004 23:50:45 -0800 (PST), Abdullah Jibaly
<amjibaly-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
wrote:> Hi all,
>
> I''m having a little trouble with the Todo tutorial at
> http://darkhost.mine.nu:81/~vince/rails/tutorial.html:
>
> 1- After adding the list and add_item methods in the
> controller, going to the todo/ url no longer takes me
> to the list page (I get an error saying the template
> for index is missing). It worked fine when the only
> line was scaffold :todo
>
> 2- After adding the add_item method to the controller,
> I get the an error because it''s trying to insert null
> into the ''done'' field, which is required. Why is this
> working in the tutorial though, and how do i fix it?
> I''m running it on sqlite and the schema is also given
> below (XP/Webrick/Rails.85):
>
> ActiveRecord::StatementInvalid in Todo#add_item
>
> todos.done may not be NULL: INSERT INTO todos (''done'',
> ''desc'') VALUES(NULL, ''qqqzzz'')
>
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/connection_adapters/abstract_adapter.rb:339:in
> `log''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:50:in
> `execute''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:45:in
> `insert''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/base.rb:776:in
> `create_without_callbacks''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/callbacks.rb:235:in
> `create''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/base.rb:761:in
> `create_or_update_without_callbacks''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/callbacks.rb:224:in
> `create_or_update''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/base.rb:647:in
> `save_without_validation''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/validations.rb:55:in
> `save_without_transactions''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/transactions.rb:115:in
> `save''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/transactions.rb:115:in
> `transaction''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/transactions.rb:86:in
> `transaction''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/transactions.rb:100:in
> `transaction''
>
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.1.0/lib/active_record/transactions.rb:115:in
> `save''
> /publicapp/controllers/todo_controller.rb:15:in
> `add_item''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:577:in
> `send''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:577:in
> `perform_action_without_filters''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/filters.rb:236:in
> `perform_action_without_benchmark''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/benchmarking.rb:30:in
> `perform_action_without_rescue''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/benchmarking.rb:30:in
> `measure''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/benchmarking.rb:30:in
> `perform_action_without_rescue''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/rescue.rb:68:in
> `perform_action''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:254:in
> `process''
>
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-0.9.5/lib/action_controller/base.rb:242:in
> `process''
> c:/ruby/lib/ruby/gems/1.8/gems/rails-0.8.5/lib/dispatcher.rb:35:in
> `dispatch''
> /public/dispatch.rb:10
> /public/dispatch.rb:1:in `load''
> /public/dispatch.rb:1
> -:7
>
> Request
>
> Parameters: {"id"=>nil,
> "new_item"=>{"desc"=>"qqqzzz"}}
>
> Schema:
>
> sqlite> .schema
> create table todos (
> id integer primary key,
> desc varchar(100) not null,
> done smallint not null );
>
> todo_controller.rb:
>
> require ''abstract_application''
>
> class TodoController < AbstractApplicationController
> helper :todo
>
> scaffold :todo
>
> def list
> @items = Todo.find_all
> end
>
> def add_item
> item = Todo.new
> item.attributes = @params["new_item"]
> if item.save
> redirect_to(:action => "list")
> else
> render_text "Couldn''t add new item!"
> end
> end
> end
>
> list.rhtml:
>
> <html>
> <head>
> <title>My todo list</title>
> </head>
>
> <body>
> <% @items.each do |@item| %>
> <%= check_box("item", "done") %>
> <%= @item.desc %>
> <%= link_to("Edit", :action => "edit", :id
=>
> @item.id) %>
> <br />
> <% end %>
>
> <hr />
>
> <form method="post" action="add_item">
> New item: <%= text_field("new_item", "desc")
%>
> <input type="submit" value="Add item" />
> </form>
> </body>
> </html>
>
> Thanks,
> Abdullah
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Vincent Foley-Bourgon
Blog: http://www.livejournal.com/~gnuvince
RSS: http://www.livejournal.com/~gnuvince/data/rss