0

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?

4
  • Your belongs_to associations are wrong FYI and also you don't have belongs_to :category in Task model. And where does entry_params come from? Commented Jul 18, 2015 at 8:37
  • Pavan is right - you need to add belongs_to :category in your task. Then in the task creation you can pass the category either by setting @task.category, or by providing the id in the task_params. Commented Jul 18, 2015 at 9:07
  • OK I changed the code- my mistake. How would I pass the id in task_params? Could you show me the line? Commented Jul 18, 2015 at 9:09
  • Did my answer worked? Commented Jul 18, 2015 at 10:47

1 Answer 1

0

First change your belongs_to associations to singular.

belongs_to :user #in category.rb
belongs_to :category #in task.rb

Then in your create method, define a instance variable for category like below

@category = Category.find(params[:category_id]) and change your create method like below

def create
  @category = Category.find(params[:category_id])
  @task = Task.new(task_params)
  @task.category_id = @category.id
  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

And make sure you have :category_id in task_params method.

Update:

You should also have a task_params method defined in your controller like below.

def task_params
  params.require(:task).permit(:id, :blobby, :challenge_id, :category_id)
end
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Pavan. When you say 'And make sure you have :category_id in task_params method' does that mean that in the task.rb I should have def task_params task_params= :category_id end?
@SebastianZeki No! In your tasks_controller, task_params, you should be having :category_id.
so task = Task.new(task_params) should be task = Task.new(:category_id) in the code above Task should be @task but can only use sign once in this box
@SebastianZeki No man! Do you have task_prams method? If so please post it in the question and i will explain.
No I don't have a task_params method. I did say in the question that I didn't know how I should do this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.