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?