Hello Guys,
Hope You are Doing Well in Your Corresponding Organizations. I am stuck in One Problem, Hope you guys can sort it out. I m explaining My Problem here:
In one of My Page I am reterivng courses values from the database. courses table contains id, course, and I m displaying those values in the form of checkboxes. Suppose If I have three courses namely B.E, MBA, B.Tech. these three values I am displaying using checkboxes.
Now What I want is if a User checks one course or two or all I want to display a sub list, which contains list of branches according to the course select in the form of checkboxes also. For that I have branches table in my database, that containsid, branch, course_id.
The Problem I am facing right now is When a I m Checking first and last box for Example B.E and B.Tech the branch related to that course is Opening Successfully. But when I am checking the middle ones indiviually or all together the Ajax is not working. I m writing My Code here. If Possible please suggest me what to do, if this method is wrong is there any other way possible to achieve this, please tell me that also.
here is my code
This is written in my views:
<% @radioname = 1 %>
<p>Course <br />
<% @courses = Course.find(:all) %>
<% @courses.each do |course| %>
<input type="checkbox" name="course_id" id="course_id + @radioname.to_s" value="<%=
course.id %>">
<%= course.course %>
<% @radioname = @radioname + 1 %>
<% end %>
</p>
<p>Branch
<div id="branch_id_container">
</div>
<%= observe_field("course_id + @radioname.to_s", :frequency => 0.25, :update => "branch_id_container", :url => { :action => :add_link_branch }, :with => "'course_id='+value") %></p>
<div id="add_branch_id_container">
</div>
<%= observe_field("course_id + @radioname.to_s", :frequency => 0.25, :update => "add_branch_id_container", :url => { :action => :add_link_branch1 }, :with => "'course_id + @radioname.to_s='+value") %></p>
and This is What I have Written in my controller:
def add_link_branch
@branches = Branch.find_all_by_course_id(params["course_id"])
end
def add_link_branch1
@branches = Branch.find_all_by_course_id(params["course_id + @radioname.to_s"])
end
and this is What I have written in app/views/add_link_branch,
<HTML>
<HEAD>
<%= javascript_include_tag "prototype" %>
</HEAD>
<BODY>
<p> <% @branches.each do |@branches| %>
<%= @html = "<input type='checkbox' id='branch_id' name='branch_id' value='#{@
branches.id}'>#{@branches.branch}" %>
<% end %>
</p>
</BODY>
</HTML>
and in app/views/add_link_branch1:-
<HTML>
<HEAD>
<%= javascript_include_tag "prototype" %>
</HEAD>
<BODY>
<p> <% @branches.each do |@branches| %>
<%= @html = "<input type='checkbox' id='branch_id' name='branch_id' value='#{@
branches.id}'>#{@branches.branch}" %>
<% end %>
</p>
</BODY>
</HTML>
One More Thing, I have defined this for four values present in the database, What to do for n values in the database, I have to use for loop, so please tell me Where i Will put for loop.
Waiting for Your Response. I m already stuck in this Task for past 4 Days.
--
___________________
Puneet Pandey
Senior Software Engineer
Safew Technology Labs
pun...@safewlabs.com
http://www.safewlabs.comhttp://puneetitengineer.wordpress.com