Hi there.
I have a problem with validating a form, where is foreign key present.
I have a table like this:
  id
  name
  password
  status_id

This is the way that status_id is implemented to the users table.
<td><%= select_tag ('status_id', options_for_select([['-- choose one
--', nil]] + @status.collect { |stat| [stat.status, stat.id]},
@user.status_id))%></td>
-------------

When I dont enter anything to any of the fields, I get an error:
 NoMethodError in Users#create
Showing users/_form.rhtml where line #12 raised:
You have a nil object when you didn't expect it! You might have expected
an instance of Array. The error occurred while evaluating nil.collect
Extracted source (around line #12):
12: <td><%= select_tag ('status_id', options_for_select([['-- choose one
--', '']] + @status.collect { |stat| [stat.status, stat.id]},
@user.status_id))%></td>
----------

And when everything is "not blank" just the status_id is nto chosen, I
get another error:
ActiveRecord::RecordNotFound in UsersController#create
Couldn't find Status with ID=
-------------

Here is the code of my controller:
class UsersController < ApplicationController
  def index
    @user = User.find(:all)
  end
 #***************************************************************** NEW
  def new
    @user = User.new
    @status = Status.find(:all)
  end

  def create
    @user = User.new(params[:user])

    status = Status.find(params[:status_id])
    @user.status = status

    if @user.save
      flash[:notice] = 'New item successfully created!'
      redirect_to(:action => 'index')
    else
      flash[:notice] = 'Creating an item failed!'
      render(:action => 'new')
    end
  end
end
-----------------

Can you suggest a solution??????
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to