I am attempting to document a join between two tables. There seems to be
no writing to my database. Please help me out it is driving me absolutly
crazy.
Basically i’ve got a table of directors that is called when the new/edit
controller model for films is activated. It shows the user a selectable
list of directors they can apply to films. The problem is in the
update/create part. I cannot figure out what to write to make it update
my directors_films database table.
Thanks so much for whomever braves this problem… I promise to return
the favor to another seeking knowledge someday!
-----schema.rb-----
ActiveRecord::Schema.define() do
create_table “directors”, :force => true do |t|
t.column “name”, :string, :default => “na”, :null => false
t.column “biography”, :text, :default => “”, :null => false
end
create_table “directors_films”, :id => false, :force => true do |t|
t.column “director_id”, :integer, :default => 0, :null => false
t.column “film_id”, :integer, :default => 0, :null => false
end
create_table “films”, :force => true do |t|
t.column “title”, :string, :default => “”, :null => false
t.column “release_date”, :date, :null => false
end
end
------director.rb-----
class Director < ActiveRecord::Base
has_and_belongs_to_many :films
end
------film.rb-------
class Film < ActiveRecord::Base
has_and_belongs_to_many :directors
end
--------application_helper.rb---------
module ApplicationHelper
def director_dropdown
select(:director, :id, Director.find(:all, :order => “name”).map {|d|
[d.name, d.director_id] })
end
end
--------films/form.rhtml-----------
Title
<%= text_field 'film', 'title' %>
Director
<%= director_dropdown %>
Release date
<%= date_select 'film', 'release_date', :order => [:day, :month, :year],
:start_year => 1900 %>
--------films_controller----------
class FilmsController < ApplicationController
def index
list
render :action => ‘list’
end
GETs should be safe (see
URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@film_pages, @films = paginate :films, :per_page => 10
end
def show
@film = Film.find(params[:id])
end
def new
@film = Film.new
@directors = Director.find_all
end
def create
@film = Film.new(params[:film])
if @film.save
flash[:notice] = ‘Film was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
def edit
@film = Film.find(params[:id])
@directors = Director.find_all
end
def update
@film = Film.find(@params[:id])
if @film.update_attributes(params[:film])
flash[:notice] = ‘Film was successfully updated.’
redirect_to :action => ‘show’, :id => @film
else
render :action => ‘edit’
end
end
def destroy
Film.find(params[:id]).destroy
redirect_to :action => ‘list’
end
end