# Model methods and partial view templates

**URL:** <https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087>\
**Category:** Rails\
**Created:** [February 18, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087 "2006-02-18T16:04:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/1 "2006-02-18T16:04:34Z")

</div>

I have a method in a controller that invokes a

render :partial =\> ‘some\_partial\_view’

In that view, I’m trying to access a method defined in a model, like  
this;

\<% for view in @an\_array %\>  
\<% local\_var = view.some\_method() %\>  
\<% another\_var = view.a\_column\_name %\>

etc…

I am getting an undefined method error but the model is accessible  
because the variable ‘another\_var’ is accessed correctly. I have tried  
the following definitions in my ‘View’ model;

def some\_method()  
…  
end

def self.some\_method()  
…  
end

I can correctly access the method from another view which is not a  
partial. But in the partial view I can’t get the method to be  
accessible.

Does anyone have any idea what is wrong with this?

–Ross

---

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/2 "2006-02-18T20:08:21Z")

</div>

Ross:

I’m a newbie, which means I’ve spent hours chasing this similar issue.

I believe if you are using \<%= render(:partial =\>  
‘get\_reordered\_images’) %\> in your index.rhtml file, you also then need  
to have a file called \_get\_reordered\_images in your index.rhtml folder  
with the following code:  
\<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>

This code then miraculously does what I thought the original index.rhtml  
code would do and, in my case, calls the “action” in my controller.rb  
file called get\_reordered\_images.

I had successfully been calling this action from this code in  
index.rhtml:  
\<%= render(:partial =\> ‘form’) %\>  
which goes to a \_form file in my index.rhtml with this:  
\<%= form\_remote\_tag(:update =\> “images\_div\_main”,  
:url =\> { :action =\> :get\_reordered\_images }  
) %\>  
How many images?  
\<%= text\_field\_tag :get\_reordered\_images %\>  
\<%= submit\_tag “Click to get images” %\>  
\<%= end\_form\_tag %\>

but I wanted to do the get\_reorder\_image stuff without an action by the  
client.

A lot of trial and error and mimicing finally got me to this solution.  
I’m hoping you can find some insight in this…and save some hours.

If I’m wrong about this and am just fooling myself, please someone  
explain why this solved a very similar problem to Ross’.

Ross Ashley wrote:

> I have a method in a controller that invokes a
> 
> render :partial =\> ‘some\_partial\_view’
> 
> In that view, I’m trying to access a method defined in a model, like  
> this;
> 
> \<% for view in @an\_array %\>  
> \<% local\_var = view.some\_method() %\>  
> \<% another\_var = view.a\_column\_name %\>
> 
> etc…
> 
> I am getting an undefined method error but the model is accessible  
> because the variable ‘another\_var’ is accessed correctly. I have tried  
> the following definitions in my ‘View’ model;
> 
> def some\_method()  
> …  
> end
> 
> def self.some\_method()  
> …  
> end
> 
> I can correctly access the method from another view which is not a  
> partial. But in the partial view I can’t get the method to be  
> accessible.
> 
> Does anyone have any idea what is wrong with this?
> 
> –Ross

---

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/3 "2006-02-18T21:12:39Z")

</div>

David T-L wrote:

> I believe if you are using \<%= render(:partial =\>  
> ‘get\_reordered\_images’) %\> in your index.rhtml file, you also then need  
> to have a file called \_get\_reordered\_images in your index.rhtml folder  
> with the following code:  
> \<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>
> 
> This code then miraculously does what I thought the original index.rhtml  
> code would do and, in my case, calls the “action” in my controller.rb  
> file called get\_reordered\_images.

Well I think the paradigm is that the controller performs some action  
and then renders a view. So unless you specifically call an action back  
in your controller, which is what render\_component does, you won’t get  
back to controller code without an action by the user.

However, that is a different problem than the one below.

> Ross Ashley wrote:
> 
> > I have a method in a controller that invokes a
> > 
> > render :partial =\> ‘some\_partial\_view’
> > 
> > In that view, I’m trying to access a method defined in a model, like  
> > this;
> > 
> > \<% for view in @an\_array %\>  
> > \<% local\_var = view.some\_method() %\>  
> > \<% another\_var = view.a\_column\_name %\>
> > 
> > etc…
> > 
> > I am getting an undefined method error but the model is accessible  
> > because the variable ‘another\_var’ is accessed correctly. I have tried  
> > the following definitions in my ‘View’ model;
> > 
> > def some\_method()  
> > …  
> > end
> > 
> > def self.some\_method()  
> > …  
> > end
> > 
> > I can correctly access the method from another view which is not a  
> > partial. But in the partial view I can’t get the method to be  
> > accessible.

The controller method is actually called from an Ajax.Updater. When I  
moved the code into the controller just to log it, the method was not  
found. The array @an\_array is stored in a global variable, since Rails  
hammers class variables, but apparently the Ajax.Updater and/or Rails is  
hammering or changing the global array also. The objects stored in the  
array are not changing during my tests. The hex id of those objects are  
not changing but the ability to call a method within those objects is  
disappearing. That is most frustrating because the only workaround I can  
think of is to stuff more info into the DB.

–Ross

---

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/4 "2006-02-18T21:46:28Z")

</div>

David T-L wrote:

> Yes, I understand the controller/view paradigm, but why do I need the  
> \_get\_reordered\_images file in my example?

Because the call to render(:partial =\> ‘get\_reordered\_images’), whether  
it happens in a controller method or in a view, as it does in you case,  
will only cause a new rhtml file to be rendered. It will not cause a new  
method in a controller to be invoked. When you created a file called  
\_get\_reordered\_images.rhtml and then put a render\_component call in it,  
a new method was invoked in your controller.

> Again, I feel your frustration

Software development consists of alternating periods of frustration and  
elation.

–Ross

---

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/5 "2006-02-18T22:06:36Z")

</div>

Okay…back to elation.

Your comment made me realize – as I had feared before – that the  
example(s) I was using were poor in that they used the “same” name in  
completely different ways, but to the newbie eye, they were the same.

So I rewrote my code to get rid of the \_get… file and replace it with  
an \_initial… file which I also used to set the $new\_search flag for  
the first time and voila…my rendered page was as desired with the flag  
acting appropriately. Ignore the flag thing, that was another  
frustration I’ve experienced.

Thanks for your help.

I guess for the newbie, I would pass along the insight:  
You cannot(should not?) call an action directly from your index.rhtml  
file, but rather, render a \_xyz file (also in the views folder) which  
calls the action and sets the html upon completion of the action.

I think where the confusion comes in for me is that my index.rhtml file  
is a “view” file so why can’t it do everything the \_xyz.rhtml file could  
do. I didn’t understand the significance until your last message.

Thanks,  
Dave

Ross Ashley wrote:

> David T-L wrote:
> 
> > Yes, I understand the controller/view paradigm, but why do I need the  
> > \_get\_reordered\_images file in my example?
> 
> Because the call to render(:partial =\> ‘get\_reordered\_images’), whether  
> it happens in a controller method or in a view, as it does in you case,  
> will only cause a new rhtml file to be rendered. It will not cause a new  
> method in a controller to be invoked. When you created a file called  
> \_get\_reordered\_images.rhtml and then put a render\_component call in it,  
> a new method was invoked in your controller.
> 
> > Again, I feel your frustration
> 
> Software development consists of alternating periods of frustration and  
> elation.
> 
> –Ross

---

<div class="post-metadata">

**Author:** ![rashley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rashley](https://www.ruby-forum.com/u/rashley)\
**Post date:** [February 18, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/model-methods-and-partial-view-templates/70087/6 "2006-02-18T21:31:26Z")

</div>

Ross,

Yes, I understand the controller/view paradigm, but why do I need the  
\_get\_reordered\_images file in my example?

Anyway, back to your issue. The reason I detailed my solution, was  
because the error message you received, is one I have gotten and it was  
very frustrating. I, too, was able to get some things to happen one  
way, but when I tried a different way, I had the pieces not quite right.

When you say the “ability” to call a method is disappearing, I’m  
guessing that you probably are not really “calling” the method when you  
think you are or that it doesn’t exist when you think it does.

For example, you said you moved the code into the controller and it  
couldn’t find the method. Aren’t methods just for classes? If not,  
isn’t it funny that you cannot evoke it when it’s in the controller?

I couldn’t “invoke” my get\_reordered\_images either until I rendered it  
“twice”, once in my index file:  
\<%= render(:partial =\> ‘get\_reordered\_images’) %\>  
and then again in my \_get\_reordered\_images file.rhtml:  
\<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>  
even though the get\_reordered\_images action (module, method?) was in my  
controller file and worked when I used:  
\<%= render(:partial =\> ‘form’) %\>  
and called it from \_form.rhtml.

Again, I feel your frustration and thought another example may give you  
a nugget of insight that helps you figure out why the method appears to  
disappear.

Good luck!

Ross Ashley wrote:

> David T-L wrote:
> 
> > I believe if you are using \<%= render(:partial =\>  
> > ‘get\_reordered\_images’) %\> in your index.rhtml file, you also then need  
> > to have a file called \_get\_reordered\_images in your index.rhtml folder  
> > with the following code:  
> > \<%= render\_component(:action =\> ‘get\_reordered\_images’) %\>
> > 
> > This code then miraculously does what I thought the original index.rhtml  
> > code would do and, in my case, calls the “action” in my controller.rb  
> > file called get\_reordered\_images.
> 
> Well I think the paradigm is that the controller performs some action  
> and then renders a view. So unless you specifically call an action back  
> in your controller, which is what render\_component does, you won’t get  
> back to controller code without an action by the user.
> 
> However, that is a different problem than the one below.
