Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Created September 10, 2012 02:06
Show Gist options
  • Save nfriend21/3688431 to your computer and use it in GitHub Desktop.
Save nfriend21/3688431 to your computer and use it in GitHub Desktop.
First, here's the full error I'm getting when I try to load the /tasks page:
NoMethodError in Tasks#index
Showing C:/Sites/task_mgr/app/views/tasks/index.html.erb where line #8 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #8):
5: <li>
6: <aside class="span9">
7: <section>
8: <%= t.content %> <span class="sub_text_list">&nbsp;&nbsp;&nbsp; <%= t.user.name %>, Due: <%= t.due_date %></span>
9: </section>
10: </aside>
11: <aside class="span2">
Rails.root: C:/Sites/task_mgr
Application Trace | Framework Trace | Full Trace
app/views/tasks/index.html.erb:8:in `block in _app_views_tasks_index_html_erb___506990488_24518844'
app/views/tasks/index.html.erb:3:in `_app_views_tasks_index_html_erb___506990488_24518844'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
**********Here's the TASKS INDEX PAGE
<h1> All Tasks <!--%= link_to "New Task", new_task_path, class: "btn btn-large btn-primary" %--> </h1>
<% @tasks.each do |t| %>
<div class="row user_list">
<li>
<aside class="span9">
<section>
<%= t.content %> <span class="sub_text_list">&nbsp;&nbsp;&nbsp; <%= t.user.name %>, Due: <%= t.due_date %></span>
</section>
</aside>
<aside class="span2">
<section>
<%= link_to 'View', t %> |
<%= link_to 'Edit', edit_task_path(t) %> |
<%= link_to "Delete", t, method: :delete , data: { confirm: "Are you sure?" } %>
</section>
</aside>
</li>
</div>
<% end %>
****************Here's the TASKS CONTROLLER
class TasksController < ApplicationController
before_filter :authorize
def index
@tasks = Task.order("due_date ASC")
end
def new
@task = Task.new
end
def create
@task = current_user.tasks.new(params[:task])
puts "PARAMS ::::: #{params.inspect}" #this allows us to see the output when running the create action.
@task.company_id = params[:company_id] #by changing the top line from "create" to "new", this allows us to pass in the company_id when this action is being done, before we save it.
puts "TASK ::::: #{@task.inspect}"
if @task.save!
flash[:success] = "Your task has been created!"
redirect_to companies_path
else
render 'new'
end
end
def show
@task = Task.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @task }
end
end
def edit
@task = Task.find(params[:id])
session[:return_to] = request.referer #this stores the requesting url (where you clicked "edit") in a session hash, which is available across multiple requests. Now you can use it in the Update action.
end
def update
@task = Task.find(params[:id])
if @task.update_attributes params[:task]
redirect_to session[:return_to] #this redirects to the requesting URL, as per the Edit action where this was initially stored.
flash[:success] = "Task was successfully updated."
else
redirect_to :back
end
end
def destroy
@task = Task.find(params[:id])
@user = @task.user_id
@task.destroy
flash[:success] = "The task has been deleted."
redirect_to user_path(current_user)
end
end
@bobbywilson0
Copy link

Task.all.map do |task|
task.user_id = 10
task.save!
end

@bobbywilson0
Copy link

Task.all.each do |task|
task.user_id = 10
task.save!
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment