Mini John
Mini John

Reputation: 7941

Questions and Answers Controller

I'm Trying to achieve, pretty much the same behavior, as the Questions from StackOverflow.

User creates a Question - other Users can Answer

I started by creating a Questions Scaffold with the appropriate relations:

Now for the Answer Part, do i have to Create a new Scaffold ? And how can i link the Answer with the specific Question?

As i'm fairly new to Rails and just getting things work, help would much be appreciated :)

Upvotes: 1

Views: 980

Answers (1)

rmagnum2002
rmagnum2002

Reputation: 11421

create answer resource:

rails g resource Answer question_id:integer content:text user_id:integer

answer.rb

belongs_to :question
belongs_to :user

question.rb

has_many :answers

user.rb

has_many :answers

the above relations will allow you to make calls like:

user.questions
question.answers
user.answers

etc..

in questions/show.html.erb

<%= @question.id %> - <%= @question.content %>

<%= form_for @question.answer.new do |f| %>
  <%= f.content %>
<% end %>

Upvotes: 1

Related Questions