Ruby / Rails:如何插入post_id w / user_id?

时间:2012-12-04 19:35:40

标签: ruby-on-rails syntax comments belongs-to posts

我读了this post,这对第一步很有帮助,现在我正处于第二步。我需要控制器和表格的帮助。

让我们说这是我的模特:

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添加到控制器的create部分,自动添加内容:

@post = current_user.post.build(params [:post])

如果评论同时属于用户和帖子,如何让它自动添加post_id以及user_id?目前,我似乎只能通过这样做来插入user_id:

current_user.comments.build(PARAMS [:评论])

我是铁杆新手。我不认为最好的方法是使字段可访问并在表单中添加隐藏字段,是不是有另一种方式?

这些是我更新的路线:

                  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

1 个答案:

答案 0 :(得分:2)

我发现做评论的最好方式已经由Ryan Bates在railscast上进行了here

class Comment < ActiveRecord::Base
  attr_accessible :content
  belongs_to :commentable, polymorphic: true
end

class Post < ActiveRecord::Base
  attr_accessible :content, :name
  has_many :comments, as: :commentable
end

您可以在控制器中实际构建注释时传递current_user,或者将其作为隐藏字段传递给您,可以使用几种不同的选项。

编辑路线: 啊,这就是为什么,你的评论和你的帖子根本没有相互勾结。在你所做的路线resources :post改变它

resources :post do 
  resources :comments 
end

如果您正在执行Post.comments.build,则需要将评论与帖子相关联  如果您正在执行current_user.comments.build,则需要在路线中执行此操作

resources :user do
  resources :comments
end