Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm not able to make a custom filter work #63

Open
bgvo opened this issue Aug 23, 2016 · 0 comments
Open

I'm not able to make a custom filter work #63

bgvo opened this issue Aug 23, 2016 · 0 comments

Comments

@bgvo
Copy link

bgvo commented Aug 23, 2016

Hi there.

I'm trying to build a custom filter for our dynamic (and multilingual) URLs. For the moment I'm just playing around to see what can I do and then design them all.

Now, one of our URLs is '/courses/search_results?course_type=5'

course_type 5 corresponds to 'Course'. I'm trying to convert the above URL to something like '/courses/search_results/Course' but I cannot make it work. This is what I've tried:

module RoutingFilter
  class Theme < Filter
    THEME_SEGMENT = %r((Course)?$)

    def around_recognize(path, env, &block)
      course_type = extract_segment!(THEME_SEGMENT, path)
      yield.tap do |params|
        params[:course_type] = CourseType.find_by_name(course_type) if course_type
      end
    end

    def around_generate(params, &block)
      course_type_id = params.delete(:course_type)
      course_type_name = CourseType.find(course_type_id).name if course_type_id
      yield.tap do |result|
        append_segment!(result, "#{course_type_name}") if course_type_name
      end
    end

  end
end

I tried to debug from extract_segment! definition:

      def extract_segment!(pattern, path)
        path.sub!(pattern) { $2 || '' }
        path.replace('/') if path.empty?
        $1
      end

This is how pattern and path (after calling sub!) look like:

pattern: //courses/search_results/(Course)/
path: "/courses/search_results/"

As I understand, that URL ("/courses/search_results/") should be recognized, but instead I got this error:

image

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant