# Using params to create new, and edit records

**URL:** <https://www.ruby-forum.com/t/using-params-to-create-new-and-edit-records/88234>\
**Category:** Rails\
**Created:** [March 9, 2007, 1:20am UTC](https://www.ruby-forum.com/t/using-params-to-create-new-and-edit-records/88234 "2007-03-09T01:20:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [March 9, 2007, 1:20am UTC](https://www.ruby-forum.com/t/using-params-to-create-new-and-edit-records/88234/1 "2007-03-09T01:20:58Z")

</div>

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.

🙂  
Mark
