0

There are two models Task and Project. A Project has many 'Task's and a 'Task' belongs_to a `Project'.

class Task < ActiveRecord::Base belongs_to :project end class Project < ActiveRecord::Base has_many :tasks end 

A list of all Tasks is displayed as a separate page for each Project and displays your Tasks.

How to create the condition for the method create, so that the Task be able to be created as an independent object, and associated with the Project?

My knowledge was only enough to write two separate methods.

To create associated objects:

def create @project = Project.find(params[:project_id]) @task = @project.tasks.create(task_params) redirect_to project_path(@project) end 

To create a separate object:

def create @task = current_user.tasks.new(task_params) respond_to do |format| if @task.save format.html { redirect_to @task, notice: 'Task was successfully created.' } format.js {} format.json { render json: @task, status: :created, location: @task } else format.html { render action: "new" } format.json { render json: @task.errors, status: :unprocessable_entity } end end end 

How to do it one method?

2
  • Checkout accepts_nested_attributes_for. Commented Feb 23, 2016 at 10:02
  • On what conditions, you want to create as independent? , If there is no Project for the project_id passed? Commented Feb 23, 2016 at 10:07

2 Answers 2

1

You have to pass in the project_id to your second method. Then you can add

@task.project_id = params[:project_id] 

or something like that. If tasks always belongs to projects you may want to model them as a nested resource.

Sign up to request clarification or add additional context in comments.

2 Comments

His question is to create task, with/without Project on the same method.
I had a hard time getting the question but it seems the questioneer was happy with the answer at least!
0

Task Controller:

def index @tasks = Task.all @task = Task.new end def create @task = if params[:project_id] @project = Project.find(params[:project_id]) @project.tasks.build(task_params) else Task.new(task_params) end ... 

Project Model:

class Project < ActiveRecord::Base has_many :tasks, dependent: :destroy accepts_nested_attributes_for :tasks, :allow_destroy => true end 

and in projects controller

private def project_params params.require(:project).permit(:name, ....., taks_attributes: [:id, .....,:_destroy]) end 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.