Forum: Ruby on Rails Model methods and partial view templates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ross A. (Guest)
on 2006-02-18 17:04
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
David T. (Guest)
on 2006-02-18 21:08
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') %></div>
which goes to a _form file in my index.rhtml with this:
     <%= form_remote_tag(:update => "images_div_main",
                         :url    => { :action => :get_reordered_images }
) %>
        <label>How many images?</label>
        <%= 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
Ross A. (Guest)
on 2006-02-18 22:12
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
David T. (Guest)
on 2006-02-18 22:31
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') %></div>
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.
>
Ross A. (Guest)
on 2006-02-18 22:46
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
David T. (Guest)
on 2006-02-18 23:06
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
This topic is locked and can not be replied to.