0

So I have an application wide helper method for breadcrumbs, that I use in the layout, across all view pages.

Here is the method below

- if request.fullpath != '/'
        .row
            .col-md-12
                #breadcrumbs
                    = link_to 'home', '/'
                    %span  »
                    - @BreadcrumbsHelper = breadcrumbs
                    - i = 0
                    - @BreadcrumbsHelper.each do |name, breadcrumb| 
                        = link_to name, breadcrumb
                        - if i != (@BreadcrumbsHelper.count - 1) 
                            %span  »
                        - i = i + 1     

From what I understand, variables in the view should be instance variables and not methods, however declaring the instance variable in the view doesn't really seem to make sense, but I am not sure how else to go about it, would it be acceptable just to leave it as a method call ie breadcrumbs.each do for example? Would that even work? What is the best practice.

EDIT (the helper, just in case it helps) :

module BreadcrumbsHelper
    def breadcrumbs
        current_path = request.fullpath
        noparam_path = current_path.split('?')

        path_parts   = noparam_path[0].split('/')

        path_parts.shift

        counter = path_parts.size
        new_paths = Hash.new

        counter.times do
            new_path = ""
            path_name = ""
            i = 0

            path_parts.each do |part|
                if i < counter
                    if new_path == ""
                        new_path = '/' + part
                    else
                        new_path = new_path + '/' + part
                    end

                    path_name = part
                    i = i + 1
                end
            end

            counter = counter -1

            #path functions

            def routeValid(new_path)
                route = "/" + new_path.gsub("_","/")

                route_valid = true
                begin
                    Rails.application.routes.recognize_path(route, :method => :get)
                rescue
                    # error means that your route is not valid, so do something to remember that here
                    route_valid = false
                end
                return route_valid
            end

            def processPath(new_path, path_name, new_paths)
                if routeValid(new_path) == true 
                    #Optional Overrides
                    if path_name == "signup"
                        path_name = "sign up"
                    end

                    new_paths[path_name] = new_path 
                end
            end

            processPath(new_path, path_name, new_paths)

        end

        new_paths = Hash[new_paths.to_a.reverse]

        return new_paths

    end
end

1 Answer 1

2

Helpers are modules that are included in the view. They are not accessed via instance variables.

You should be able to access a method defined in a helper directly in a view. So rather than writing

- @BreadcrumbsHelper = breadcrumbs
- i = 0
- @BreadcrumbsHelper.each do |name, breadcrumb|

you would just write something like

- breadcrumbs.each do |name, breadcrumb|

You will also probably want to capture the count of breadcrumbs before the loop, with something like

- my_breadcrumbs = breadcrumbs
- breadcrumbs_count = my_breadcrumbs.size
- my_breadcrumbs.each do |name, breadcrumb|

and replacing

@BreadcrumbsHelper.count

with

breadcrumbs_count
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.