Hi again,
Well I’m struck at place since several days.
I have a relationship such that many services could be dependent on a
service. I have listed all the dependent services of a service on the
show page. Now just below the list of dependents I have provided a text
field with auto complete and a button so that a user can add new
dependents to the service. My problem is that I’m unable to read the
value from the text field and insert into MTM table.
To make things clearer I’ve following table structure for the self join:
services
id
.
.
service_dependencies
service_id
dependent_service_id
impact
severity
I’ve following Model
service.rb
has_many :service_dependencies
has_many :dependents, :through => :service_dependencies
service_dependency.rb
belongs_to :service
belongs_to :dependent, :class_name => ‘Service’, :foreign_key =>
‘dependent_service_id’
Before controller I would tell you what I’m trying to do in my show
view. Apart from displaying all the fields in services table
correspoding to the service being displayed currently I have displayed
all the dependents in following manner
<% for depend in @service.dependents %>
<%= link_to depend.send(“service_name”), :action => ‘show’, :id =>
depend.id %>
<% dependency = @service.service_dependencies.find(:first, :conditions
=>
[“dependent_service_id = ?”, depend.id])%>
<%=h dependency.send(“impact”) %
<%=h dependency.send(“severity”) %>
<%end%>
In order to let users’ add more dependents I’m providing text box with
autocomplete in following manner:
Dependent Service
Name
<%= text_field_with_auto_complete 'service_dependents', 'service_name',
{:size => 20}, { :after_update_element => 'auto_complete_on_select' }%>
Now I want to create a button Add Dependent and pass the value of the
service that is displayed at present and the value of the new dependent
service that the user had entered in the text box above. Something like
this
<%= button_to "Add Dependent", :action => 'add_dependent', :id => @service, :param => ?? %>
In the controller I have written the body of add_dependent in following
manner:
def add_dependent
serv = @service.find(params[:id])
depend = @service.find(params[:param])
@service_dependency = ServiceDependency.new(:service => serv,
:dependent => depend)
if @service_dependency.save
flash[:notice] = ‘Dependent added successfully.’
redirect_to :action => ‘show’, :id => serv
else
render :action => ‘show’, :id => serv
end
end
If you are still reading, you must have understood my problem. My
problem is, how would I refer to the value of dependent service entered
in the text box and pass it to the add_dependent method?
Similarly, how would I get the values of impact and severity in text
boxes and pass it to my add_dependent method.
Thanks in anticipation.