ruby on rails - Each statement cannot iterate through current_user.id -
I am currently trying to list a todo list of logged in user. I'm using devise as my certification gem
The code is like this:
Class Todd Controller & lt; ApplicationController def index if user_signed_in? @todo_items = Todo.all.find_by_user_id (current_user.id) @new_todo = Todo.new other redirect_to new_user_session_path end and end
But when I run it, I get this error
Exception method # for each & # lt; Todo: 0x38b8d68 & gt;
My Transaction Statement:
& lt; Div class = "well" & gt; & Lt;% @ todo_items.each do | T | & Gt%; ** # Error in this line ** & lt; Li & gt; & Lt;% = t.todo_item% & gt; & Lt; / Li & gt; & Lt;% end% & gt; & Lt; / Div & gt;
I do not know what I'm doing wrong I have also tried
@todo_items = Todo.all.find_by_user_id (params [current_user.id ])
I still get the same error I am completely new on this and I am not using any tutorials so far for this so please answer your reply to a new View from the perspective. Thanks
when you are turning it on
Todo.all .find_by_user_id (Current_user.id)
First of all,
Todo.all will return all user records
And you find_by_user_id apply on Todo.all, as we know that gives a record return according to the method
Obviously,
Todo.all.find_by_user_id
will return a single record, because you are searching for a single user from all users.
And you are trying to repeat through the same record which is causing the error.
& lt;% @ Todo_items.each do | T | & Gt%; ** # Error in this line ** & lt; Li & gt; & Lt;% = t.todo_item% & gt; & Lt; / Li & gt; & Lt;% end% & gt;
Try a different way,
Todo.find_all_by_user_id (current_user.id) (or) Todo Where (: user_id = & gt; current_user.id) (or) Todo.all (: where = & gt; "user_id = & gt; # {current_user.id}")
Any of the above will do the work.
Comments
Post a Comment