Skip to content

Instantly share code, notes, and snippets.

@nfriend21
Created September 10, 2012 23:37
Show Gist options
  • Save nfriend21/3694818 to your computer and use it in GitHub Desktop.
Save nfriend21/3694818 to your computer and use it in GitHub Desktop.
I changed my table "companies", to "projects" and went through and updated all the references to company/companies. Everything is working fine, except I am again getting that same validation error when I try to create a new task on the projects/4/tasks/new. It's not getting the project_id and therefore the validation is failing. If I hard code the params[:project_id] with the actual number of the project, it creates fine.
I tried all the same things we did yesterday and the day before, to no avail.
**********Here's the error message:
ActiveRecord::RecordInvalid in TasksController#create
Validation failed: Project can't be blank
Rails.root: C:/Sites/task_mgr
Application Trace | Framework Trace | Full Trace
app/controllers/tasks_controller.rb:18:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"r5zSOGhUgqwvoMf3Fw/DuDXTReYdAg1IeXZSXTyfpng=",
"task"=>{"content"=>"ghgjghg",
"due_date(1i)"=>"2012",
"due_date(2i)"=>"9",
"due_date(3i)"=>"10"},
"commit"=>"Update Task"}
Show session dump
Show env dump
Response
Headers:
None
************Here's the 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.project_id = params[:project_id]#by changing the top line from "create" to "new", this allows us to pass in the project_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 projects_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
*************Here's the 3 models:
class Task < ActiveRecord::Base
attr_accessible :content, :due_date
belongs_to :user
belongs_to :project
validates :content, presence: true
validates :user_id, presence: true
validates :project_id, presence: true
#accepts_nested_attributes_for :user, :project
end
class Project < ActiveRecord::Base
attr_accessible :name, :users_attributes
has_many :tasks, dependent: :destroy
belongs_to :user
validates :name, presence: true
accepts_nested_attributes_for :user, :tasks
end
class User < ActiveRecord::Base
has_secure_password
attr_accessible :name, :email, :password, :password_confirmation, :project_id
has_many :tasks, dependent: :destroy
has_many :projects
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
#below not needed anymore, due to has_secure_password
#validates :password_confirmation, presence: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment