开发者

Ruby on Rails - enquiring about passing parameters

开发者 https://www.devze.com 2023-04-10 23:33 出处:网络
I am creating a simple RoR application where I can create posts and also comment on them.My thought on how to do this is to have posts and comments be of the same type and utilizing the same MVC excep

I am creating a simple RoR application where I can create posts and also comment on them. My thought on how to do this is to have posts and comments be of the same type and utilizing the same MVC except comments have a parent id. Posts will have a parent id of 0 while comments will have the id of its parents. I was thinking of when I tried to create a comment, I could just pass in the parent's id to the new method. This did not work. Although I got no errors it seems like the @post variable in the new method is not the same @post variab开发者_如何学编程le in the create method. My intuition was that the new method creates a new object then passes the object to the view. The view then populates the objects parameters and then sends it to the create method and the create method saves the object to the database. From trial and error this does not seem to be the case unless I am just doing it wrong. Is there an easier way to get the same functionality I am trying to achieve? or is there a way to get my way to work? any help would be greatly appreciated!


I would recommend you follow the classic "Build a Blog in 15 minutes" video that shows you how to do this.

Among other things, since a post has_many :comments I wouldn't recommend doing what you're proposing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号