Hi, im new to rails and I got a mind boggling issue. I have a select form populated with Institutes names e.g University of Yale. These records are obtained from the Institute ActiveRecord.The institutes table has a many to many relationships with the course table. My application should work such that when you select a certain institute using a select() component and press the button proceed to view courses available for the institute chosen. This will take you to a new page with the courses. My problem is that i don''t know how i will store selected institute id and use it to select specific courses using the many to many relationships.. below are my code snippets =========================================================================/app/controller/Course.rb class CourseController < ApplicationController layout ''standard'' def list @courses = Course.find(:all) end def show @course = Course.find(params[:id]) end def new @courses = Course.new @institutes = Institute.find(:all) # @cfilters = CourseFilter.find(params[:id]) end def create @course = Course.new(params[:course]) if @course.save redirect_to :action => ''list'' else flash[:warning]="Failed to " @courses = Course.find(:all) render :action => ''new'' end end end =========================================================================/app/views/institute/list.html.erb <% if @institutes.blank? %> <p>There are not any institutes currently in the system.</p> <% else %> <p><u>Select your chosen Institution</u></p> <form action="/course/show" method="submit" id = "course_id"> <%= select ("submit", "name", Institute.find(:all).collect {|c| [ c.name, c.id ] }, :prompt => "Select Institute", :onchange => "this.form.submit();")%> <%= submit_tag "Proceed To Courses"%> </form> <%end%> <p><%= link_to "Add new College", {:action => ''new'' }%></p> ======================================================================/app/views/courses/list.html.erb <% if @courses.blank? %> <p>There are not any courses currently in the system.</p> <% else %> <p><u>Select your chosen Course</u></p> <ul id="courses"> <% Course.find(params[:id]).each do |c| %> <li><%= link_to c.course_name, :action => "show", :id => c.id %></li> <% end %> </ul> <%= collection_select(:course,:id,@courses,:id,:course_name) %></p> </ul>--> <form action="/unit/list" method="submit"> <%= submit_tag "Proceed" %> </form> <% end %> <p><%= link_to "Add new College", {:action => ''new'' }%></p> ====================================================================== Would really need you help people. Thanx -- Posted via http://www.ruby-forum.com/. -- 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.
Got the same problem myself and I''m yet to crack it! -- Posted via http://www.ruby-forum.com/. -- 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.
There are few issue in this: 1. You should not be doing sql queries directly from views, instead set the instance variable in controller and pass it to view. 2. Your form is submitting to /course/show, instead it should be /course/list 3. Controller name should be plural, you have got the name as Course Controller, but the corresponding views directory is "courses" 4. To follow along with rails standards, you should be name of your "list" action should be "index" 5. Your select field should set "institute", "id" instead of "select", "name" 6. Course list action should filter the course list based on the institute id, something like Course.find_by_institute_id(params[:institute][:id]) Fix these and maybe you will figure out a solution. Chirag http://sumeruonrails.com On Thu, Aug 18, 2011 at 10:10 PM, kevin ng''eno <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, im new to rails and I got a mind boggling issue. > > I have a select form populated with Institutes names e.g University of > Yale. These records are obtained from the Institute ActiveRecord.The > institutes table has a many to many relationships with the course table. > > My application should work such that when you select a certain institute > using a select() component and press the button proceed to view courses > available for the institute chosen. This will take you to a new page > with the courses. > > My problem is that i don''t know how i will store selected institute id > and use it to select specific courses using the many to many > relationships.. below are my code snippets > =========================================================================> /app/controller/Course.rb > > class CourseController < ApplicationController > layout ''standard'' > def list > @courses = Course.find(:all) > end > def show > @course = Course.find(params[:id]) > end > def new > @courses = Course.new > @institutes = Institute.find(:all) > # @cfilters = CourseFilter.find(params[:id]) > end > def create > @course = Course.new(params[:course]) > if @course.save > redirect_to :action => ''list'' > else > flash[:warning]="Failed to " > @courses = Course.find(:all) > render :action => ''new'' > end > end > end > > =========================================================================> /app/views/institute/list.html.erb > > <% if @institutes.blank? %> > <p>There are not any institutes currently in the system.</p> > <% else %> > <p><u>Select your chosen Institution</u></p> > <form action="/course/show" method="submit" id = "course_id"> > <%= select ("submit", "name", Institute.find(:all).collect {|c| [ > c.name, c.id ] }, :prompt => "Select Institute", :onchange => > "this.form.submit();")%> > > <%= submit_tag "Proceed To Courses"%> > </form> > <%end%> > > <p><%= link_to "Add new College", {:action => ''new'' }%></p> > ======================================================================> /app/views/courses/list.html.erb > <% if @courses.blank? %> > <p>There are not any courses currently in the system.</p> > <% else %> > <p><u>Select your chosen Course</u></p> > <ul id="courses"> > <% Course.find(params[:id]).each do |c| %> > <li><%= link_to c.course_name, :action => "show", :id => c.id %></li> > <% end %> > </ul> > <%= collection_select(:course,:id,@courses,:id,:course_name) %></p> > </ul>--> > <form action="/unit/list" method="submit"> > <%= submit_tag "Proceed" %> > </form> > <% end %> > <p><%= link_to "Add new College", {:action => ''new'' }%></p> > ======================================================================> > Would really need you help people. Thanx > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. > >-- 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.
Chirag Singhal wrote in post #1017338:> There are few issue in this:Thanx alot Chirag, let me try out your hints . Will get back to you -- Posted via http://www.ruby-forum.com/. -- 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.
Hello guys! I am new in rails. I am querying the courses table for specific courses that belong to specific institute using this sql statement. I''m getting the same courses for all the institutes that I have. Any help on what to put in place of 1 or how to restructure my query will be highly appreciated. Here is my query! <%= select ("course_code", "course_name",Course.find_by_sql("select i.name, c.course_name from institutes as i, courses as c , institute_courses as ic where i.id = ic.institute_id and c.id = ic.course_id and i.id = 1;").collect {|c| [ c.course_name, c.id ] })%> -- Posted via http://www.ruby-forum.com/. -- 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.
On Fri, Aug 19, 2011 at 9:36 AM, Charles Otieno <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am querying the courses table for specific courses that belong to > specific institute using this sql statement.Why are you writing SQL statements instead of taking advantage of associations, one of Rails'' most powerful features? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.