views:

583

answers:

3

I have 2 tables: form, questions. Idea is very easy, every form have many question. Tables were maded

form = | id | title |

questions = | id | title | input | form_id |

and how you can guess the form_id is key of form id.

class FormsController < ApplicationController
 active_scaffold :form
end

class QuestionsController < ApplicationController
 active_scaffold :question
end

class Question < ActiveRecord::Base
 has_one :form
end

class Form < ActiveRecord::Base
 has_many :question
end

and I want to make an activescaffold (question) with select with avalable forms. Now I can only input the id of form, but not choice it by dropdown menu. How should I configure rails or activescaffold?

Thanks. Sorry for my English :)

+2  A: 

You need to add some configuration to your controller.

class QuestionsController < ApplicationController
  active_scaffold :question do |config|
    config.columns = [:id, :title, :input, :form_id]
    config.columns[:form_id].ui_type = :select
  end
end
Sarah Mei
A: 

I'm not sure about active_scaffold but there are a few mistakes in your relationship statements in the form and question model. I think this is what you want:

class Form < ActiveRecord::Base
 has_many :questions
end

class Question < ActiveRecord::Base
 belongs_to :form
end

Hope this helps.

allesklar
+1  A: 

If you wish to get an dropdown working with activescaffold without a headache, you must follow this simple rule: Dropdown will works only when you refer to association declared in belongs_to. Making a mix of previous answers you can:

in your models follow the rails way. Respect pluralizations and inflections. then write them as suggest allesklar:

class Form < ActiveRecord::Base
 has_many :questions
end

class Question < ActiveRecord::Base
 belongs_to :form
end

It's very important to use singular names in associations with "belongs_to", in this case :form

In your controller use the answer of Sarah Mei, but change it a little bit, using the association name that you have declare in belongs_to, then you'll have something like this

class QuestionsController < ApplicationController
  active_scaffold :question do |config|
    config.columns = [:id, :title, :input, :form]
    config.columns[:form].ui_type = :select
  end
end

If u use models with more than one field, remember to use "def to_label", because ActiveScaffold can't guess wich field you want to show.

Greets