Inserting to a table that has a foreign key

Hi,

I’m new to ruby on rails and I’m working on a very small project. I’m in
the very early stages of development and I’m already stuck. Here’s the
problem:

I have two tables:

programs

  • program_id primary key auto_increment
  • program_name

modules

  • module_id primary key auto_increment
  • module_code
  • module_name
  • program_id foreign key

My first steps in coding are to do a simple admin section, where the
user can add new programs and modules. The ‘adding new programs’
functionality works, but the ‘adding new modules’ errors with the
following message: ‘wrong number of arguments (1 for 0)’. I’m guessing
this has something to do with the fact that program_id is a foreign in
the modules table. Can anybody advise on where I’m going wrong please?


Models:

class Program < ActiveRecord::Base
has_many :modules
end

class Module < ActiveRecord::Base
belongs_to :program
end


add_module View:

<% @page_title = “AddModule” -%>
<%= start_form_tag(:action => “save_module”) %>



Module Code:
<%= text_field(“module”, “module_code”, “size” => 40 ) %>



Module Name:
<%= text_field(“module”, “module_name”, “size” => 40 ) %>



Program Name:
<%= collection_select(“module”, “program_id” , Program.find_all,
“program_id”, “program_name”) %>




<%= submit_tag(" ADD ") %>
<%= end_form_tag %>


Controller:

def add_module
end

def save_module
@module = Module.new(params[:module])

if @module.save
  flash[:notice] = "Module '" + @module.module_name + "' was

sucessfully added!"
redirect_to :action => ‘list’
else
redirect_to :action => “add_module”
end
end


Development Log:

Processing AdminController#save_module (for at 2007-05-08 16:35:06)
[POST]
Session ID: 61f30f5fc79afbce0720f9470ffc7766
Parameters: {“module”=>{“module_code”=>“Module X”, “program_id”=>“3”,
“module_name”=>“New Module”}, “commit”=>" ADD ",
“action”=>“save_module”, “controller”=>“admin”}

ArgumentError (wrong number of arguments (1 for 0)):

Trish Mulligan wrote:

Processing AdminController#save_module (for at 2007-05-08 16:35:06)
[POST]
Session ID: 61f30f5fc79afbce0720f9470ffc7766
Parameters: {“module”=>{“module_code”=>“Module X”, “program_id”=>“3”,
“module_name”=>“New Module”}, “commit”=>" ADD ",
“action”=>“save_module”, “controller”=>“admin”}

ArgumentError (wrong number of arguments (1 for 0)):

In the browser it should tell you on which line this error is occurring,
might help narrow it down?

Sorry, I should’ve said. The error occurs on the following line…

@module = Module.new(params[:module])

Thanks,

Trish.

Trish Mulligan wrote:

Sorry, I should’ve said. The error occurs on the following line…

@module = Module.new(params[:module])

Thanks,

Trish.

Perhaps it is because Module is a reserved word in Ruby, or rather a
ruby class. When you call Module.new it is trying to create a new module
object.

http://www.ruby-doc.org/core/classes/Module.html

Try changing the name of your model to something else.

Thanks for taking the time to reply.

I have changed the name of my model and it now works.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs