开发者

Rails one-to-many and forms

Im starting off with Rails and was trying to implement a basic Q & A website. One question has many answers.

How do I create a new question that attaches to an existing answer?

All the available tutorials on 开发者_Python百科the web are for nested forms, which is not what im looking for. The question already exists. I just want to attach new answers to it.

Any help much appreciated!


I think you can do this so.

class Question < ActiveRecord::Base
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
end

Routes:

resources :questions do
  resources :answers
end

Here: /questions/1/answers/new
Controller: Answer Action: new

return an HTML form for creating a new ad belonging to a specific question

POST /questions/1/answers
create create a new ad belonging to a specific question

http://guides.rubyonrails.org/routing.html#nested-resources

https://github.com/rusak1 - send a letter for me here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜