I am trying to implement HMT for the first time and having a ton of
trouble. I would like to be able to have the user check a checkbox
and fill some extra fields for data input. I have tried so many
iterations borrowed from different places I found on the web, none
seem to work quite right. Essentially I have two models:
''practice''
and ''system'' where a practice can have one or many systems and
I have
a join model called ''implementation'' where I also store some
additional attributes such as who implemented the software and when.
I have now rolled all my code back to the basics following the HABTM
railscast. So right now when I check a checkbox (choosing the system)
the join model (implementation) does get both system ID''s but I cant
figure out how to correctly add the additional 3 fields and have them
stored when I save. I have included below what I think is relevant, I
am beyond just hints at this point, I need help with the code itself -
just cant get it to work. THANK YOU THANK YOU if you can help
-------PRACTICE MODEL-------
class Practice < ActiveRecord::Base
attr_accessible :name, :tax_id, :location_ids, :employee_ids, :system_ids
has_and_belongs_to_many :employees
has_and_belongs_to_many :locations
has_many :implementations
has_many :systems, :through => :implementations
validates_presence_of :name, :tax_id
end
-----SYSTEM MODEL-------
class System < ActiveRecord::Base
attr_accessible :system_publisher, :system_name, :system_type, :system_version,
:system_version_certified, :practice_ids
has_many :implementations
has_many :practices, :through => :implementations
end
-----IMPLEMENTATION MODEL-----
class Implementation < ActiveRecord::Base
attr_accessible :system_start_date, :system_stop_date, :system_implemented_by,
:system_ids, :practice_ids
belongs_to :system
belongs_to :practice
end
----PRACTICE CONTROLLER----
class PracticesController < ApplicationController
#added for auto complete text (HABTM)
#auto_complete_for :location, :name
#auto_complete_for :employee, :first_name
def index
@practices Practice.name_like_all(params[:search].to_s.split).ascend_by_name
#@practices = Practice.all
end
def show
@practice = Practice.find(params[:id])
end
def new
@practice = Practice.new
end
def create
@practice = Practice.new(params[:practice])
if @practice.save
flash[:notice] = "Successfully created practice."
redirect_to @practice
else
render :action => ''new''
end
end
def edit
@practice = Practice.find(params[:id])
end
def update
params[:practice][:employee_ids] ||= []
params[:practice][:location_ids] ||= []
#?
# params[:practice][:system_ids] ||= []
#?
@practice = Practice.find(params[:id])
if @practice.update_attributes(params[:practice])
flash[:notice] = "Successfully updated practice."
redirect_to @practice
else
render :action => ''edit''
end
end
def destroy
@practice = Practice.find(params[:id])
@practice.destroy
flash[:notice] = "Successfully destroyed practice."
redirect_to practices_url
end
end
PRACTICE _form
<% form_for @practice do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :tax_id %><br />
<%= f.text_field :tax_id %>
</p>
<p>
<strong>Employee(s):</strong>
<% for employee in Employee.find(:all) %>
<div>
<%= check_box_tag "practice[employee_ids][]", employee.id,
@practice.employees.include?(employee) %>
<%= employee.first_name %> <%= employee.last_name %>
</div>
<% end %>
</p>
<p>
<strong>Location(s):</strong>
<% for location in Location.find(:all) %>
<div>
<%= check_box_tag "practice[location_ids][]", location.id,
@practice.locations.include?(location) %>
<%= location.name %>
</div>
<% end %>
</p>
<% for system in System.find(:all) %>
<div>
<%= check_box_tag "practice[system_ids][]", system.id,
@practice.systems.include?(system) %>
<%= system.system_name %>
</div>
<% end %>
<p><%= f.submit %></p>
<% end %>
--
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-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.