【问题标题】:NoMethodError in Associated model's form object关联模型的表单对象中的 NoMethodError
【发布时间】:2019-08-12 21:15:35
【问题描述】:

我正在构建一个项目管理应用程序。 我的系统就像-一个项目有很多功能,一个功能有很多任务。 而且 routes.rb 被定义为

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

第一级,即项目到功能对于新功能表单工作正常,但是当我尝试将新任务表单实现为 ​​->

  <%= 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 %>

现在显示错误为

这是我的模型->

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

任务的控制器看起来像->

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

非常感谢任何帮助 - 我已经被这个错误困扰了好几天了。

【问题讨论】:

    标签: ruby-on-rails forms ruby-on-rails-5 associations


    【解决方案1】:

    如果您尝试运行 $ rails routes,您会明白为什么您当前的路线会失败。

                       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)    
    

    您必须致电:

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

    一个更好的主意是取消嵌套路由。您可以使用shallow option

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

    或者,如果您出于某种原因想要保留嵌套功能的成员路由(显示、编辑、更新、销毁),也可以这样做:

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

    【讨论】:

    • 附带说明最好在控制器中执行form_for([@feature, @task], ...) 并分配@task = @feature.tasks.new。它允许您在创建、编辑和更新操作中重复使用表单。
    • 如果我使用浅嵌套,那么 form_for 会是什么样子?
    • 我尝试了第一种方法,也尝试了你的 cmets,但每次都显示undefined method tasks' for nil:NilClass`
    • 与您原来的问题完全一样。使用rails routes 检查输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多