Forum: Ruby on Rails Using params to create new, and edit records

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
856832b6952733df3f15d8372ee1cd74?d=identicon&s=25 Mark Hankey (tvpow)
on 2007-03-09 01:20
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:

<h1>New question</h1>

<% form_tag :action => 'create' do %>
  <label for="question_type">Question type</label></br>
  <select id="question_type" name="question[question_type_id]">
    <% for qt in QuestionType.find(:all) %>
      <option value=<%= %>><%= %></option>
    <% 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

def create
  @question =[:question])

    flash[:notice] = 'Question was successfully created.'
    redirect_to :action => 'list'
    render :action => 'new'

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

Even if I set the question_type_id like so in the controller create
@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:
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.

This topic is locked and can not be replied to.