1

したがって、次のコードが原因で何かが欠けていると思います。エラーメッセージを含めました。私にこのエラー。

ルート

   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

コメント コントローラ

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"}
4

2 に答える 2

2

私が思うに、タスク テーブルには task_id がありませんが、タスクに task_id を追加するために移行を実行すると、このエラーが発生します

あなたは間違ったテーブルについて考えています。あなたがこれを言うとき:

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

commentsActiveRecord は、テーブルにテーブルtask_idにリンクするための列があると想定していtasksます。追加するのに移行は必要ありません。tasks.task_id追加するのに移行が必要ですcomments.task_id

ActiveRecord Associations Guideは参考になるかもしれません。

于 2012-09-03T20:35:55.150 に答える
1

試してみてください

class Comment < ActiveRecord::Base  
  belongs_to :task
end

tasktasks1対1の対応ではありません。

ここでbelongs_toアソシエーションについてもっと読む:

属する

于 2012-09-03T20:45:43.307 に答える