gpt4 book ai didi

ruby-on-rails - 未定义的属性 Task_ID

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:14 25 4
gpt4 key购买 nike

所以我认为由于下面的代码我遗漏了一些东西,我已经包含了错误消息,我认为任务表没有 task_id,但是当我运行迁移以将 task_id 添加到任务时,它仍然给了我这个错误。

路线

   resources :tasks do
resources :comment
end

模型 - 评论

class Comment < ActiveRecord::Base  
attr_accessible :author, :comment
belongs_to :tasks
has_one :author
validates :comment, :presence => true
end

模型任务

class Task < ActiveRecord::Base
attr_accessible :name, :task
has_many :comments, :dependent => :destroy
validates :name, :presence => true
end

评论 Controller

class CommentsController < ApplicationController
def index
@comment = Comment.new
@comments = Comment.all
end

def create
@task = Task.find(params[:id])
@comment = @task.Comment.create(params[:task])
redirect_to post_path(@task)
end

end

形式 - 部分

<%= form_for ([@task, @task.comments.build]) do |f| %>
<%= f.text_area :comment %>
<%= f.submit %>
<% end %>

有什么问题?

unknown attribute: task_id

Extracted source (around line #1):

1: <%= form_for ([@task, @task.comments.build]) do |f| %>
2: <%= f.text_area :comment %>
3: <%= f.submit %>
4: <% end %>

Trace of template inclusion: app/views/tasks/show.html.erb

Rails.root: /home/adam/Documents/Aptana Studio 3 Workspace/StartPoint
Application Trace | Framework Trace | Full Trace

app/views/forms/_comments.html.erb:1:in `_app_views_forms__comments_html_erb___445541804__622889658'
app/views/tasks/show.html.erb:5:in `_app_views_tasks_show_html_erb___428009053_87363420'

Request

Parameters:

{"id"=>"3"}

最佳答案

what I am think is that tasks table doesn't have a task_id, but when I rad a migration to add task_id to tasks, it still gave me this error

您正在考虑错误的表。当你这样说时:

class Comment < ActiveRecord::Base  
belongs_to :task # Note that this should be singular as zeacuss notes below
end

ActiveRecord 假定 comments 表将有一个 task_id 列链接回 tasks 表。您不需要迁移来添加 tasks.task_id,您需要迁移来添加 comments.task_id

ActiveRecord Associations Guide可能会有所帮助。

关于ruby-on-rails - 未定义的属性 Task_ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com