really strange error in.... rails

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

really strange error in.... rails

Post by Luke »

I am building a really basic todo list application in rails to learn the framework and I am coming across the strangest error. In the app, i click "create new todo" and it pops up a little box for me to put in a todo item. i enter the todo summary and click "add" and it adds it. now i repeat that same thing, and not only does it not add the todo, but it deletes the other one. now i add another and it goes in, but if i add another both are deleted again. this goes on infinitely. :?

here is my controller

Code: Select all

class TodosController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @todo_pages, @todos = paginate :todos, :per_page => 100, :order => 'priority DESC', :conditions => {:date_completed => nil}
    @completed_pages, @completed = paginate :todos, :per_page => 10, :order => 'date_completed DESC'
  end

  def show
    @todo = Todo.find(params[:id])
  end

  def new
    @todo = Todo.new
  end

  def create
    @todo = Todo.new(params[:todo])
    if @todo.save
      flash[:notice] = 'Todo was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @todo = Todo.find(params[:id])
  end

  def update
    @todo = Todo.find(params[:id])
    if @todo.update_attributes(params[:todo])
      flash[:notice] = 'Todo was successfully updated.'
      redirect_to :action => 'show', :id => @todo
    else
      render :action => 'edit'
    end
  end

  def destroy
    Todo.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def complete
    @todo = Todo.find(params[:id])
    @todo.date_completed = DateTime.now.strftime "%Y-%m-%d %H:%M:%S"
    @todo.save
    redirect_to :action => 'list'
  end
end
and my views

list view

Code: Select all

<h2>Todos</h2>

<ul id="todo-list">

<% for todo in @todos %>

  <li><a href="<%= url_for :action => 'complete', :id => todo %>" class="complete"><%=h todo.title %></li>

    <!-- <td><%= link_to 'Show', :action => 'show', :id => todo %></td>
    <td><%= link_to 'Edit', :action => 'edit', :id => todo %></td>
    <td></td> -->

<% end %>

</ul>

<%= link_to 'Previous page', { :page => @todo_pages.current.previous } if @todo_pages.current.previous %>
<%= link_to 'Next page', { :page => @todo_pages.current.next } if @todo_pages.current.next %> 

<br />

<div id="addform" class="hide"><%= render 'todos/ajax_new' %></div>

<p><a id="new-todo" accesskey="n" href="<%= url_for :action => 'new' %>#TB_inline?width=200&height=140&inlineId=addform" class="thickbox">New Todo</a></p>

<h3>Completed todos</h3>

<ul id="completed-list">

<% for complete in @completed %>

  <li> <%=h complete.title %> </li>

<% end %>

</ul>
add view

Code: Select all

<h1>New todo</h1>

<% form_tag :action => 'create' do %>

<!--[form:todo]-->
<p><label for="todo_title" class="hide">Title</label><br/>
<%= text_field 'todo', 'title'  %></p>

<%= hidden_field 'todo', 'date_completed', :value => '' %>

<%= hidden_field 'todo', 'priority', :value => '0' %>
<!--[eoform:todo]-->
  
  <%= submit_tag "Create" %>
<% end %>
and my model

Code: Select all

class Todo < ActiveRecord::Base
  validates_presence_of :title, :priority
end
any help would be appreciated.
Post Reply