Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Julie Torres
1,522 PointsRails Todo App: Error Entering Content in New Todo Item
I'm getting an error when I try to add a new Todo Item. When I click save, the error message appears saying the content is blank, even though I have filled in valid content. The associated tests are also failing.
Here's my Todo list form partial:
<% if @todo_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todo_item.errors.count, "error") %>
prohibited this todo item from being saved:</h2>
<ul>
<% @todo_item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<%end %>
<%= form.label :content %>
<%= form.text_field :content %>
<%= form.submit "Save" %>
And my New Todo Item view:
<h1><%= @todo_list.title %> --Adding Todo List Item </h1>
<%= form_for [@todo_list, @todo_item] do |form| %>
<%= render partial: form %>
<% end %>
Can anyone spot a problem in these documents? If not, what else could be causing the problem?
Julie Torres
1,522 PointsSure! I wonder if I need to be passing in parameters to the new method? I tried passing in todo_items_params, but that was triggering errors as well.
class TodoItemsController < ApplicationController
before_action :find_todo_list
def index
end
def new
@todo_item = @todo_list.todo_items.new
end
def create
@todo_item = @todo_list.todo_items.create(params[:id])
if @todo_item.save
flash[:success] = "Added Todo List Item"
redirect_to todo_list_todo_items_path
else
flash[:error] = "Todo item could not be added."
render action: :new
end
end
def edit
@todo_item = @todo_list.todo_items.find(params[:id])
end
def update
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.update_attributes(todo_item_params)
flash[:success] = "Saved todo list item"
redirect_to todo_list_todo_items_path
else
flash[:error] = "That todo item could not be saved"
render action: :edit
end
end
def destroy
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.destroy
flash[:success] = "Todo list item was deleted."
else
flash[:error] = "Todo list item could not be deleted."
end
redirect_to todo_list_todo_items_path
end
def complete
@todo_item = @todo_list.todo_items.find(params[:id])
@todo_item.update_attribute(:completed_at, Time.now)
redirect_to todo_list_todo_items_path, notice: "Todo item marked complete"
end
def url_options
{ todo_list_id: params[:todo_list_id]}.merge(super)
end
private
def todo_item_params
params[:todo_item].permit(:content)
end
def find_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
end
2 Answers
Andrew Molloy
37,259 PointsJust double check all your embedded Ruby has <%= %> including the equals sign.
Julie Torres
1,522 PointsI figured it out!
This line
@todo_item = @todo_list.todo_items.create(params[:id])
should have been
@todo_item = @todo_list.todo_items.create(todo_item_params)
Thank you, Andrew and Maciej, for taking a look at it.
Maciej Czuchnowski
36,441 PointsMaciej Czuchnowski
36,441 PointsCan you show us the controller code for items?