、、の3つのモデルPost
がReplies
ありUser
ます。Reply
のネストされたリソースPost
:
replies.rb:
class Reply < ActiveRecord::Base
attr_accessible :content
belongs_to :user
belongs_to :post, :counter_cache => true
.
.
.
役職。rb:
class Post < ActiveRecord::Base
include ActionView::Helpers
attr_accessible :title, :content
belongs_to :user
has_many :replies, dependent: :destroy
.
.
.
これは私が私のsample_data.rake
ファイルに持っているものです:
sample_data.rake:
def make_users
admin = User.create!(name: "Example User",
email: "example@railstutorial.org",
password: "foobar",
password_confirmation: "foobar")
admin.toggle!(:admin)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
end
def make_posts
users = User.all(limit: 6)
50.times do
title = Faker::Lorem.sentence(1)
content = Faker::Lorem.sentence(5)
users.each { |user| user.posts.create!(title: title,
content: content) }
end
end
これが私が返信を作成する方法です:
def create
@post = Post.find(params[:post_id])
@reply = @post.replies.build(params[:reply])
@reply.user_id = current_user.id
if @reply.save
# do this
else
# do this
end
end
特定の数の投稿に返信を入力するにはどうすればよいですか?