Hi all, please i wanna call method from controller in view but it through this exception ////// NoMethodError in Summery#index undefined method `listProjects'' for #<#<Class:0x468645c>:0x4686434> ///// and I''m trying to call it in code <%@userProjects = listProjects(user.id)%> so please if anyone tell me how to solve it, i will be appreciated. regards, Mohamed -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The best thing to do would be to place this line in the controller where ''listProjects'' would be in scope. ERB templates have (form a Ruby point of view) a very odd local scope where they inherit instance variables from the calling controller, but not the calling controllers scope. MCV purest will tell you just not to do logic like this in the view. From the name of your function ity looks like listProject might best be done in the user model as "has_many :projects" and in the view: <%@userProjects = user.projects%> If list projects is returning HTML then this is a classic helper method (the generator should have made you a helper file named after you controller in the RAILS_ROOT/app/helper Directory) Summary: * Best answer to the question asked: Assign @userProject in controller * More effective: Move this functionality to model class * For HTML output: One of the above + method in helper John Miller Mohamed Saeed wrote:> Hi all, > > please i wanna call method from controller in view but it through this > exception > > ////// > NoMethodError in Summery#index > undefined method `listProjects'' for #<#<Class:0x468645c>:0x4686434> > ///// > > and I''m trying to call it in code > > <%@userProjects = listProjects(user.id)%> > > so please if anyone tell me how to solve it, i will be appreciated. > > regards, > Mohamed-- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Mohamed, as John Miller points out this is easier done from the controller and also recommended as a best practice. Here''s a simple (un-tested) example of how to assign and access instance variables (@...) from a controller and view: Class SummeryController < ApplicationController def list @user = User.find(1) # if you''re using act_as_authenticate, # you could have something here like: user = self.current_user @userProjects = Projects.find(:all, :conditions => ["user_id = ?", @user.id]) end list.rhtml <% for project in @userProjects %> <%= project.name %><br/> <% end %> Regards, Dave _______________________________ Information and Educational Technology Kwantlen University College - 604-599-2120 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org wrote on 21-08-2007 09:40:33 AM:> > Hi all, > > please i wanna call method from controller in view but it through this > exception > > ////// > NoMethodError in Summery#index > undefined method `listProjects'' for #<#<Class:0x468645c>:0x4686434> > ///// > > and I''m trying to call it in code > > <%@userProjects = listProjects(user.id)%> > > so please if anyone tell me how to solve it, i will be appreciated. > > regards, > Mohamed > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David Dumaresq wrote:> Hi Mohamed, as John Miller points out this is easier done from the > controller and also recommended as a best practice. Here''s a simple > (un-tested) example of how to assign and access instance variables > (@...) > from a controller and view: > > Class SummeryController < ApplicationController > > def list > @user = User.find(1) # if you''re using act_as_authenticate, > # you could have something here > like: user = self.current_user > @userProjects = Projects.find(:all, :conditions => ["user_id = > ?", > @user.id]) > end > > list.rhtml > <% for project in @userProjects %> > <%= project.name %><br/> > <% end %> > > Regards, > Dave > _______________________________ > Information and Educational Technology > Kwantlen University College - 604-599-2120Agreed. That kind of logic really begins in the controller. I just went through the exercise myself while implementing application-wide record filtering. A user can create/select a filter to use, which can be based on a project, a scenario, the last change date, or any permutation of the three. As a proof of concept, I created my filter model, scaffolded it, then hacked logic into the scenarios list view. Didn''t take long to see that the view was the wrong place to do it. My clean list.rhtml got horribly cluttered. Once I moved that record selection logic to the controller, I was back to my nice clean rhtml in the view. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
each action in controller should have it''s view so why don''t you use <%= render :partial ... %> or <%= render_component ... %> in your case listProjects (actually should be list_projects) must be class method of your Project model; class Project < ActiveRecord::Base def list_projects( user = nil ) Project.find_by_user_id user end end cheers On Aug 21, 7:40 pm, Mohamed Saeed <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all, > > please i wanna call method from controller in view but it through this > exception > > ////// > NoMethodError in Summery#index > undefined method `listProjects'' for #<#<Class:0x468645c>:0x4686434> > ///// > > and I''m trying to call it in code > > <%@userProjects = listProjects(user.id)%> > > so please if anyone tell me how to solve it, i will be appreciated. > > regards, > Mohamed > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---