A
A
Anton Ivanov2015-07-13 02:39:59
Ruby on Rails
Anton Ivanov, 2015-07-13 02:39:59

How to properly reorder named routes in rails 4?

Hello.
There is an application on rails. And the main site running this application. All links on this site are without CNC.
We make a personal website for one of our clients based on our application (the design is his own, the functionality is ours).
The client's desire is to have CNC links. It is not possible to change links globally, as they are also used outside of our site. Ok, we create duplicates of our routes using the prefix. That is, there was a route named index, a new one appeared with the name clientname_index, and so on.
But many actions use helper calls for named routes. Putting conditions everywhere about which helper to call - it smells like a collective farm :)
Therefore, it was decided to add the following construction to application_controller:

def set_template_and_vars
    if '%clients_domain%' == request.host
      params[:template] = 'clients_template'

      %w(index project ...куча других имен маршрутов).each do |name|
        self.class.send('define_method', ("#{name}_path")) { |arguments| send("clientname_#{name}_path", arguments) }
      end

    end
  end

but, for some reason, it seems that it can be done more correctly.
What are the disadvantages of this solution and how to do it more correctly?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vsuhachev, 2015-07-13
@vsuhachev

Set conditions everywhere about which helper to call - it smells like a collective farm :)

It is necessary to pass a certain variable to the routing helpers, which will determine how routes are calculated depending on. For a client, it will contain a value; for your organization, it will be empty. In my opinion this is the most correct way.

V
Viktor Vsk, 2015-07-13
@viktorvsk

Please, at least in Rails, don't port this CNC machine.
What is the problem? Have you heard
about this method ? Does not help?
And also about globs and constraints ?
Write what you want to achieve and why you can't do it with the standard Rails methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question