Call controller methods from view

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

Hi Mohamed, as John M. 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 %>

<% end %>

Regards,
Dave


Information and Educational Technology
Kwantlen University College - 604-599-2120

David D. wrote:

Hi Mohamed, as John M. 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 %>

<% end %>

Regards,
Dave


Information and Educational Technology
Kwantlen University College - 604-599-2120

Agreed. 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.

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 S. [email protected]

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 M.

Mohamed S. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs