URL Routing Application

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












In a Ruby on Rails application I want to have this routing:



  • for URL www.example.com/reports go to controller documents and action reports

  • for URL www.example.com/charts go to controller documents and action charts

In the routes.rb file I currently have:



scope controller: :documents do
get 'reports' => :reports
get 'charts' => :charts
end


The code works as expected, but is there a way so I keep the code dry and don't repeat the action names?







share|improve this question



























    up vote
    1
    down vote

    favorite












    In a Ruby on Rails application I want to have this routing:



    • for URL www.example.com/reports go to controller documents and action reports

    • for URL www.example.com/charts go to controller documents and action charts

    In the routes.rb file I currently have:



    scope controller: :documents do
    get 'reports' => :reports
    get 'charts' => :charts
    end


    The code works as expected, but is there a way so I keep the code dry and don't repeat the action names?







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In a Ruby on Rails application I want to have this routing:



      • for URL www.example.com/reports go to controller documents and action reports

      • for URL www.example.com/charts go to controller documents and action charts

      In the routes.rb file I currently have:



      scope controller: :documents do
      get 'reports' => :reports
      get 'charts' => :charts
      end


      The code works as expected, but is there a way so I keep the code dry and don't repeat the action names?







      share|improve this question













      In a Ruby on Rails application I want to have this routing:



      • for URL www.example.com/reports go to controller documents and action reports

      • for URL www.example.com/charts go to controller documents and action charts

      In the routes.rb file I currently have:



      scope controller: :documents do
      get 'reports' => :reports
      get 'charts' => :charts
      end


      The code works as expected, but is there a way so I keep the code dry and don't repeat the action names?









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 26 at 20:16









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 24 at 5:43









      True Soft

      1085




      1085




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You could do the following. (I assume you already have a resourceful route for your documents right?)



          resources :documents do
          collection do
          get 'reports'
          get 'charts'
          end
          end


          To take advantage of the nested routing provided by rails (see the docs)



          Which will generate the following urls:



           Prefix Verb URI Pattern Controller#Action
          reports_documents GET /documents/reports(.:format) documents#reports
          charts_documents GET /documents/charts(.:format) documents#charts


          If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:



          scope controller: :documents do
          %w(reports charts).each do |action|
          get action
          end
          end


          But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.






          share|improve this answer























          • Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
            – Daniel
            May 28 at 13:37










          • @Coal_ sorry, first time I do code review and not answering on stackoverflow.
            – siegy22
            May 28 at 13:47











          • Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
            – True Soft
            Jun 5 at 11:25

















          up vote
          0
          down vote













          If you choose to use non-restful actions, you will have to define all of the custom actions.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195063%2furl-routing-application%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            You could do the following. (I assume you already have a resourceful route for your documents right?)



            resources :documents do
            collection do
            get 'reports'
            get 'charts'
            end
            end


            To take advantage of the nested routing provided by rails (see the docs)



            Which will generate the following urls:



             Prefix Verb URI Pattern Controller#Action
            reports_documents GET /documents/reports(.:format) documents#reports
            charts_documents GET /documents/charts(.:format) documents#charts


            If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:



            scope controller: :documents do
            %w(reports charts).each do |action|
            get action
            end
            end


            But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.






            share|improve this answer























            • Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
              – Daniel
              May 28 at 13:37










            • @Coal_ sorry, first time I do code review and not answering on stackoverflow.
              – siegy22
              May 28 at 13:47











            • Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
              – True Soft
              Jun 5 at 11:25














            up vote
            3
            down vote



            accepted










            You could do the following. (I assume you already have a resourceful route for your documents right?)



            resources :documents do
            collection do
            get 'reports'
            get 'charts'
            end
            end


            To take advantage of the nested routing provided by rails (see the docs)



            Which will generate the following urls:



             Prefix Verb URI Pattern Controller#Action
            reports_documents GET /documents/reports(.:format) documents#reports
            charts_documents GET /documents/charts(.:format) documents#charts


            If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:



            scope controller: :documents do
            %w(reports charts).each do |action|
            get action
            end
            end


            But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.






            share|improve this answer























            • Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
              – Daniel
              May 28 at 13:37










            • @Coal_ sorry, first time I do code review and not answering on stackoverflow.
              – siegy22
              May 28 at 13:47











            • Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
              – True Soft
              Jun 5 at 11:25












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            You could do the following. (I assume you already have a resourceful route for your documents right?)



            resources :documents do
            collection do
            get 'reports'
            get 'charts'
            end
            end


            To take advantage of the nested routing provided by rails (see the docs)



            Which will generate the following urls:



             Prefix Verb URI Pattern Controller#Action
            reports_documents GET /documents/reports(.:format) documents#reports
            charts_documents GET /documents/charts(.:format) documents#charts


            If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:



            scope controller: :documents do
            %w(reports charts).each do |action|
            get action
            end
            end


            But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.






            share|improve this answer















            You could do the following. (I assume you already have a resourceful route for your documents right?)



            resources :documents do
            collection do
            get 'reports'
            get 'charts'
            end
            end


            To take advantage of the nested routing provided by rails (see the docs)



            Which will generate the following urls:



             Prefix Verb URI Pattern Controller#Action
            reports_documents GET /documents/reports(.:format) documents#reports
            charts_documents GET /documents/charts(.:format) documents#charts


            If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:



            scope controller: :documents do
            %w(reports charts).each do |action|
            get action
            end
            end


            But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited May 28 at 13:51


























            answered May 28 at 13:25









            siegy22

            1463




            1463











            • Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
              – Daniel
              May 28 at 13:37










            • @Coal_ sorry, first time I do code review and not answering on stackoverflow.
              – siegy22
              May 28 at 13:47











            • Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
              – True Soft
              Jun 5 at 11:25
















            • Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
              – Daniel
              May 28 at 13:37










            • @Coal_ sorry, first time I do code review and not answering on stackoverflow.
              – siegy22
              May 28 at 13:47











            • Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
              – True Soft
              Jun 5 at 11:25















            Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
            – Daniel
            May 28 at 13:37




            Code dumps ('try this:') are not allowed as answers here. Please add what this does and why it is better than OPs solution.
            – Daniel
            May 28 at 13:37












            @Coal_ sorry, first time I do code review and not answering on stackoverflow.
            – siegy22
            May 28 at 13:47





            @Coal_ sorry, first time I do code review and not answering on stackoverflow.
            – siegy22
            May 28 at 13:47













            Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
            – True Soft
            Jun 5 at 11:25




            Your example works. You're right about readability, but I was just wondering how to do it, as I tried but I was unable to make it work.
            – True Soft
            Jun 5 at 11:25












            up vote
            0
            down vote













            If you choose to use non-restful actions, you will have to define all of the custom actions.






            share|improve this answer

























              up vote
              0
              down vote













              If you choose to use non-restful actions, you will have to define all of the custom actions.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                If you choose to use non-restful actions, you will have to define all of the custom actions.






                share|improve this answer













                If you choose to use non-restful actions, you will have to define all of the custom actions.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered May 26 at 13:09









                ogirginc

                1484




                1484






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195063%2furl-routing-application%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation