I have asked a similar question but the answer involved using current_user. This question is a little bit different. I have a simple database where users can create a category. Each category has many tasks. I would like the user to click on a category to see the tasks for that category. The relationship between user and category is 1 to many and the relationship between category and task is one to many as well.
The problem I am having is in the creation of the foreign key in the task so it can tell which category it belongs to. The task create occurs when a user clicks a category
My model is as follows
category.rb
class Category < ActiveRecord::Base
has_many :tasks
belongs_to :users
end
task.rb
class Task < ActiveRecord::Base
belongs_to :categories
end
The controller
def create
@task = Task.new(task_params)
respond_to do |format|
if @task.save
format.html { redirect_to @task, notice: 'Task was successfully created.' }
format.json { render :show, status: :created, location: @task }
render :layout => false
else
format.html { render :new }
format.json { render json: @task.errors, status: :unprocessable_entity }
render :layout => false
end
end
end
I think the problem is what I use for task_params. Currently I have no method for this. How do I get the category foreign key inserted into task when a user creates a task?
belongs_toassociations are wrong FYI and also you don't havebelongs_to :categoryinTaskmodel. And where doesentry_paramscome from?