Share controller method when rendering partial

View: New views
6 Messages — Rating Filter:   Alert me  

Share controller method when rendering partial

by andkjaer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,
I have a question regarding rendering partials:

I have this controller called twitter

class TwitterController < ApplicationController

  def index
    @twitter_search = Twitter::Search.new('#rails')
  end
end

In the twitter index views i render a partial:

<%= render :partial => "shared/twitterlist" %>

This work's fine for the Twitter index view, but when I call the
render partial from another view like dashboard/index like this:

<%= render :partial => "shared/twitterlist" %>

I get this error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

What am I doing wrong here?
Is it even possible to share data across the views like that?


--~--~---------~--~----~------------~-------~--~----~
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Share controller method when rendering partial

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


andkjaer wrote:

> Hi,
> I have a question regarding rendering partials:
>
> I have this controller called twitter
>
> class TwitterController < ApplicationController
>
>   def index
>     @twitter_search = Twitter::Search.new('#rails')
>   end
> end
>
> In the twitter index views i render a partial:
>
> <%= render :partial => "shared/twitterlist" %>

First of all you're relying on implicit instance variables in your
partials. You really shouldn't do that. The current version of Rails
automatically injects instance variable assigned in the controller into
partials. I believe, however, that this behavior has been deprecated and
may be removed in future versions of Rails. Instead you should
explicitly pass local data into the partial with :locals =>
@twitter_search, or similar technique.

> This work's fine for the Twitter index view, but when I call the
> render partial from another view like dashboard/index like this:
>
> <%= render :partial => "shared/twitterlist" %>
>
> I get this error:
>
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.each

If you need that data available to pass into the shared partial then you
need to assign that data in the controller calling used for rendering
the view/partial.

> What am I doing wrong here?
> Is it even possible to share data across the views like that?

Keep in mind that instance variables set in controllers are only
available for one request/response cycle. Any data you need for a view
you should assign in the controller action used to render 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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Share controller method when rendering partial

by Adam Akhtar-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Robert Walker wrote:
> Keep in mind that instance variables set in controllers are only
> available for one request/response cycle. Any data you need for a view
> you should assign in the controller action used to render the view.

Also remember that before_filter can be used in cases where you need to
load data for multiple actions within a controller. This will help keep
your code DRY.
--
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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Share controller method when rendering partial

by andkjaer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks Robert,
But one question.
Where do I put the code called by an :before_filter
In the application_controller ?

On 2 Nov., 15:54, Robert Walker <rails-mailing-l...@...>
wrote:

> Robert Walker wrote:
> > Keep in mind that instance variables set in controllers are only
> > available for one request/response cycle. Any data you need for a view
> > you should assign in the controller action used to render the view.
>
> Also remember that before_filter can be used in cases where you need to
> load data for multiple actions within a controller. This will help keep
> your code DRY.
> --
> 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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Share controller method when rendering partial

by Euwyn Poon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If I'm not mistaken, you have TwitterController and a DashboardController.  You could have a before_filter (to set @twitter_search) in each of these for the methods that result in the partial being rendered.  I suppose you could also have a before_filter in the ApplicationController, but not sure that's the best idea.

Euwyn


On Mon, Nov 2, 2009 at 10:31 AM, andkjaer <andkjaer.net@gmail.com> wrote:

Thanks Robert,
But one question.
Where do I put the code called by an :before_filter
In the application_controller ?

On 2 Nov., 15:54, Robert Walker <rails-mailing-l...@...>
wrote:
> Robert Walker wrote:
> > Keep in mind that instance variables set in controllers are only
> > available for one request/response cycle. Any data you need for a view
> > you should assign in the controller action used to render the view.
>
> Also remember that before_filter can be used in cases where you need to
> load data for multiple actions within a controller. This will help keep
> your code DRY.
> --
> 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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Share controller method when rendering partial

by andkjaer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


But where shhould the method live? In the Application controller?

def twitter_search
   ...
end

On 2 Nov., 23:21, Euwyn Poon <euwynp...@...> wrote:

> If I'm not mistaken, you have TwitterController and a DashboardController.
> You could have a before_filter (to set @twitter_search) in each of these for
> the methods that result in the partial being rendered.  I suppose you could
> also have a before_filter in the ApplicationController, but not sure that's
> the best idea.
>
> Euwyn
>
> On Mon, Nov 2, 2009 at 10:31 AM, andkjaer <andkjaer....@...> wrote:
>
> > Thanks Robert,
> > But one question.
> > Where do I put the code called by an :before_filter
> > In the application_controller ?
>
> > On 2 Nov., 15:54, Robert Walker <rails-mailing-l...@...>
> > wrote:
> > > Robert Walker wrote:
> > > > Keep in mind that instance variables set in controllers are only
> > > > available for one request/response cycle. Any data you need for a view
> > > > you should assign in the controller action used to render the view.
>
> > > Also remember that before_filter can be used in cases where you need to
> > > load data for multiple actions within a controller. This will help keep
> > > your code DRY.
> > > --
> > > 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@...
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---