私はこの投稿を読みました、そしてそれは最初のステップのために役に立ちました、今私は2番目のステップにいます。コントローラーとフォームについてサポートが必要です。
これが私のモデルだとしましょう:
User
has_many :posts
has_many :comments
Post
belongs_to :user
has_many :comments
Comment
belongs_to :user
belongs_to :post
そして、これらは私のテーブルです:
User
id
Post
id
user_id
Comment
id
user_id
post_id
最初の投稿では、ユーザーIDは、コントローラーの作成セクションに追加することで、コンテンツとともに自動的に追加されます。
@post = current_user.post.build(params [:post])
コメントがユーザーと投稿の両方に属している場合、post_idとuser_idを自動的に追加するにはどうすればよいですか?現在、私はこれを行うことによってのみuser_idを挿入するように見えることができます:
current_user.comments.build(params [:comment])
Railsは初めてです。フィールドにアクセスできるようにして、フォームに非表示のフィールドを追加するのが最善の方法ではないと思いますが、別の方法はありませんか?
これらは私の更新されたルートです:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
Post_comments GET /posts/:Post_id/comments(.:format) comments#index
POST /posts/:Post_id/comments(.:format) comments#create
new_Post_Comment GET /posts/:Post_id/comments/new(.:format) comments#new
edit_Post_Comment GET /posts/:Post_id/comments/:id/edit(.:format) comments#edit
Post_Comment GET /posts/:Post_id/comments/:id(.:format) comments#show
PUT /posts/:Post_id/comments/:id(.:format) comments#update
DELETE /posts/:Post_id/comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_Post GET /posts/new(.:format) posts#new
edit_Post GET /posts/:id/edit(.:format) posts#edit
Post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
start /start(.:format) posts#new