Achy97
Achy97

Reputation: 1024

NoMethodError in Associated model's form object

I am building a project management app. My System is like- A project has many features, A feature has many tasks. And the routes.rb is defined as

  resources :projects do
    resources :features do
      resources :tasks
    end
  end

The first level , that is project to feature is working fine for new feature form , but when I try to implement new tasks form as ->

  <%= form_for([@feature, @feature.tasks.build], class: "form-group row") do |form| %>

    <%= form.label :name %>
    <%= form.text_field :name, required:true, class: "form-control" %>

    <%= form.submit class: "btn btn-primary m-2" %>

  <% end %>

Now its showing error as

enter image description here

Here is my models->

class Task < ApplicationRecord
  belongs_to :feature
end
class Feature < ApplicationRecord
  belongs_to :project
  has_many :tasks
end

And the controller for tasks looks like->

class TasksController < ApplicationController  
  before_action :set_feature

  def new
    @task = @feature.tasks.new
  end

  def create
    @task = @feature.tasks.new(task_params)

    if @task.save
       redirect_to project_features_path
    else
      render :new
    end
  end

  def edit
  end

  def update

    if @task.update(task_params)

      redirect_to project_feature_tasks_path
    else
      render :edit
    end

  end

  def complete
    @task.update(completed: true)
    redirect_to project_feature_tasks_path
  end

  def destroy
    @feature.task.destroy
    redirect_to project_feature_tasks_path
  end


  private

  def set_feature
    @feature = Feature.find(params[:feature_id])
  end

  def task_params
    params.require(:task).permit(:name,:completed, :project_id,:feature_id)
  end

end

Any help is very much appreciated - I am stuck with this error for days now.

Upvotes: 0

Views: 67

Answers (1)

max
max

Reputation: 102250

If you try running $ rails routes you can see why your current routes are failing you.

                   Prefix Verb   URI Pattern                                                                              Controller#Action
    project_feature_tasks GET    /projects/:project_id/features/:feature_id/tasks(.:format)                               tasks#index
                          POST   /projects/:project_id/features/:feature_id/tasks(.:format)                               tasks#create
 new_project_feature_task GET    /projects/:project_id/features/:feature_id/tasks/new(.:format)                           tasks#new
edit_project_feature_task GET    /projects/:project_id/features/:feature_id/tasks/:id/edit(.:format)                      tasks#edit
     project_feature_task GET    /projects/:project_id/features/:feature_id/tasks/:id(.:format)                           tasks#show
                          PATCH  /projects/:project_id/features/:feature_id/tasks/:id(.:format)                           tasks#update
                          PUT    /projects/:project_id/features/:feature_id/tasks/:id(.:format)                           tasks#update
                          DELETE /projects/:project_id/features/:feature_id/tasks/:id(.:format)    

You would have to call:

form_for([@project, @feature, @feature.tasks.build], ...) do |form|

A better idea is to unnest the routes. You can do this by using the shallow option:

resources :projects do
  resources :features, shallow: true do
    resources :tasks
  end
end

Or you can do it like so if you for some reason want to keep the member routes (show, edit, update, destroy) for features nested:

resources :projects do
  resources :features
end

resources :features, only: [] do
  resources :tasks
end

Upvotes: 1

Related Questions