2 Replies Latest reply on Apr 9, 2013 2:14 PM by Dhepthi LakshmiNarasimhan

    Helpers for cleaning up the view

      Hi

       

      I would like to clear up the view so I was thinking to move to header buttons to a helper method. For instance I would like to move this code into a helper called show_sync

       

           <% if SyncEngine::logged_in > 0 %>

               <a href="<%= url_for :controller => :Settings, :action => :do_sync %>" class="ui-btn-left" data-icon="refresh">

                Sync

           </a>

           <a href="<%= url_for :controller => :Settings, :action => :logout %>" class="ui-btn-right" data-icon="star">

             Logout

           </a>

           <% else %>

               <a href="<%= url_for :controller => :Settings, :action => :login %>" class="ui-btn-right" data-icon="star">Login</a>

           <% end %>

       

       

      I tried to put this in a method in application_helper.rb or browser_helper.rb but none of the options seems to work

       

      def show_sync

      ...

      end

       

      any ideas how can I extract this lines out to a helper please?

       

      thank you

      Mariano

        • Re: Helpers for cleaning up the view
          Mark Nongkhlaw

          You could download my app, where I had used a helper method to determine the designations of employees based on their age. Though that might sound a little wiered, its just to demo how we can define our own functions and use them in the views :

           

          https://developer.motorolasolutions.com/message/8024#8024

           

          Hope it helps.

          • Re: Helpers for cleaning up the view

            Hi Mariano

             

            While moving code to rb file from erb, you have to remove embedded tags. You can try the below code

             

            def header_data

               content = ""

                content += "<h1>Home</h1>"

                 if SyncEngine::logged_in > 0

                    content +="<a href='/app/Settings/do_sync' class='ui-btn-left' data-icon='refresh'>Sync</a>"

                  else

                      content += "<a href='/app/Settings/login' class='ui-btn-left' data-icon='refresh'>Login</a>"

                  end

                content

              end

             

            Placed the code in application helper file. You must include the helper in the necessary controller file like

             

            require 'helpers/application_helper'

            include ApplicationHelper

             

            Now you can call the helper method in erb file as

            <div data-role="header" data-position="inline">

               <%= header_data %>

              </div>

             

            Hope this might be helpful

             

            Another effective way of cleaning the view is to move the code to partials and render the partial wherever needed. See http://docs.rhomobile.com/rhodes/ui#advanced-usage-of-render for rendering partials