URL Routing Application
Clash 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 actionreports
- for URL www.example.com/charts go to controller
documents
and actioncharts
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?
ruby ruby-on-rails url-routing
add a comment |Â
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 actionreports
- for URL www.example.com/charts go to controller
documents
and actioncharts
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?
ruby ruby-on-rails url-routing
add a comment |Â
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 actionreports
- for URL www.example.com/charts go to controller
documents
and actioncharts
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?
ruby ruby-on-rails url-routing
In a Ruby on Rails application I want to have this routing:
- for URL www.example.com/reports go to controller
documents
and actionreports
- for URL www.example.com/charts go to controller
documents
and actioncharts
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?
ruby ruby-on-rails url-routing
edited May 26 at 20:16
Jamalâ¦
30.1k11114225
30.1k11114225
asked May 24 at 5:43
True Soft
1085
1085
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
up vote
0
down vote
If you choose to use non-restful actions, you will have to define all of the custom actions.
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
up vote
0
down vote
If you choose to use non-restful actions, you will have to define all of the custom actions.
add a comment |Â
up vote
0
down vote
If you choose to use non-restful actions, you will have to define all of the custom actions.
add a comment |Â
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.
If you choose to use non-restful actions, you will have to define all of the custom actions.
answered May 26 at 13:09
ogirginc
1484
1484
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password