Using params to create new, and edit records


#1

Hi all,

I’m working on my first RoR project and it’s slowly coming together. I
am having a problem getting a new object initialised from the params
being sent back from the form. It goes like this…

I have Questions and there are three types of questions. My questions
table links to a table question_types via a foreign key in the questions
table ‘question_types_id’ and I’ve declared that questions belongs_to
:question_type and question_type has_many :questions.
So far so good, I can reference the members using dot notation fine, but
the default scaff doesn’t give me a drop down list so I edited the view
for the new action so it looks like this:

New question

<% form_tag :action => ‘create’ do %>

Question type <% for qt in QuestionType.find(:all) %> ><%= qt.name %> <% end %> <%= render :partial => 'form' %> <%= submit_tag "Create" %> <% end %>

<%= link_to ‘Back’, :action => ‘list’ %>

But when my controller tries to save it using the default static
scaffold:

def create
@question = Question.new(params[:question])

if @question.save
flash[:notice] = ‘Question was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

it fails saying “1 error prohibited this question from being saved”
I have in the question model “validates_presence_of :question_type_id”
and if I remove it then it fails saying that the question_type_id cannot
be null.
So it seems that the question_type_id is not being set.
On the lighttpd server I see the params listed correctly as I would
hope.

Even if I set the question_type_id like so in the controller create
action:
@question.question_type_id = 2 # this is a valid value
then it still gives the same errors above. grr.

The wierd thing is that I’m using pretty much the same code for the edit
action, and that seems to work fine where the call:
@question.update_attributes(params[:question])
seems to get all of the information required and updates the type id
field correctly.

any ideas or suggestions or pointers to stuff gratefully accepted.

:slight_smile:
Mark