8 Replies Latest reply on Apr 2, 2013 5:17 AM by John Michael Vincent Rustia

    Can we include erb file into another erb file?

      Now I am thinking of reusing the view file. For example several page will use the same tab bar at the footer.

      To make easier maintain and remove duplication, is it possible I create one footer.erb file and include it into different view.erb file?

        • Re: Can we include erb file into another erb file?
          BHAKTA RANJAN SATAPATHY

          Hi,

          I think you can add your header and footer HTML code to /app/layout.erb

           

          So it will be available to across all the pages.

           

          Thanks

          • Re: Can we include erb file into another erb file?
            Jon Tara

            You can use partials, similar to Rails partials. See "Advanced Usage of Render"

             

            http://docs.rhomobile.com/rhodes/ui

              • Re: Can we include erb file into another erb file?

                Thanks a lot Jon. I can imagine we can use this feature do lots of things.

                Here I have more questions:

                1. based on the document, it does not mention where to call rendering the partials:

                    render :partial => "ad", :collection => ["foo1","foo2","foo3"]

                   can we call it on both on controller and view?

                 

                  When I check the Rails document, I found lots of samples which render the partial on the view, I haven't found a case for the controller.

                 

                2.  In the view, is it possible to render different partials on the different controllers? For example, I have a home page, it will display 3 different things, which data comes from 3 different controllers, so I wonder if the partial rendering will support controller,

                like this:

                 

                  render :partial => "partial_controller1", :controller => "controller1" ...

                 

                Thanks

                  • Re: Can we include erb file into another erb file?
                    Jon Tara

                    Partials are, by definition, a part of a page. As such, they only make sense when called from a view. I'm not sure what the use case would be for calling from a controller.

                     

                    Data doesn't come from controllers. It comes from models. A view can get data from any model. So, for example, let's say you have an "order" view, but the order includes details about the customer. So, you need data from the Customer model as well as from the Order model.

                     

                    So, let's say you want to display an order. You will have a "show" method in your Order controller. The "show" method should gather all of the data needed to display the Order view, which includes data from both the Order and Customer models. It puts that data in instance (@) variables so that it can be accessed by the view.

                     

                    You can, in fact, share partials between controllers. Views (and partials) only care that they are provided the necessary inputs (instance variables and/or locals). So, you could re-use the same partial in the Order view and in a Customer view. You will just have to specify the path to the partial to use it with a different controller.

                  • Re: Can we include erb file into another erb file?

                    One more question.

                     

                    Can we get the sample code of using the render partial feature?

                     

                    Thanks

                  • Re: Can we include erb file into another erb file?

                    Hi guys,

                     

                    I have used partial rendering in project but while deploying on android i found an issue

                     

                    I am not able to open partial render page, i found the reason it is giving error " No such file directory to load ...data/data/someurl.com/rhodata/apps/partials/_dyncamic.erb.iseq ..

                     

                    it is working fine on window s mobile.

                    How to set corrent url for calling partial page

                     

                    render :partial =>"partials/label", :locals => { :ctrlid => item.object } %>

                    • Re: Can we include erb file into another erb file?

                      I tried this code in my layout.erb to include several erbs always in the page

                       

                        <body>

                          <%= render :controller => :Misc, :partial => 'Misc/ajax_cancel_div'%>

                          <%= render :controller => :Misc, :partial => 'Misc/modal_dialog'  %>

                          <%= render :controller => :Misc, :partial => 'Misc/search_result' %>

                        </body>

                       

                      You must make sure you have the controller and the erb existing in the folder with the name specified in the :controller.

                      It works on me, you could try this in your application.

                      1 of 1 people found this helpful