Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Render a Controller action in another view


  • Please log in to reply
3 replies to this topic

#1 magnus

magnus

    Passenger

  • Members
  • 5 posts

Posted 12 September 2013 - 02:47 PM

Hello

This is my first Rails project - so my apologies if my description / question is "blurry".

 

In short:

1. I have two Controllers: courses and static_pages.

 

2. I want to render the index-action from the Courses controller on the static_pages home view page.

 

Is this possible (without breaking the DRY rule)? Should I (can you?) move the index view (for Courses controller) to a partial?

 

I have tried this - but is it not working (no errors - no render of index action):

 

Courses controller:

def index
    @courses = Course.all
end

Static_pages controller have a (empty) home action, which I changed to this:

 

def home
    @courses = Course.all
end
 
And then, in the home view:
<%= yield %>
 
The root is set to
root 'static_pages#home'

Sorry for the fuzzy explanation ;)

 

Best regards,

Magnus



#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 12 September 2013 - 04:18 PM   Best Answer

def home
   
@courses = Course.all
end
This is fine...
 
 
And then, in the home view:
<%= yield %>

 

but Yield what? 

 

What I think you need to do is in your 'static_pages#home',  you need to create a loop (@courses.each do |c|.... ) to display the contents of your @courses.  

 

You can put that @courses loop section into a partial file, and render it from your static_pages@home.  



#3 magnus

magnus

    Passenger

  • Members
  • 5 posts

Posted 13 September 2013 - 06:53 AM

Thanks Rowel.

I put the loop in a partial, and then:

<%= render "courses/courseslist" %>

and it is working perfect :)

 

// Magnus



#4 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 13 September 2013 - 05:27 PM

You're welcome. 

Still a newb, but sometimes I feel like I'm beginning to understand this Rails thingy :) 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users