rails 4 routing, remove default para - ruby-on-rails

I have some problem with rails 4 routing. Currently the routing is like this.
analytic_practiceday_path POST /analytics/:analytic_id/practiceday(.:format) analytics#practiceday
analytic_practiceuser_path GET /analytics/:analytic_id/practiceuser(.:format) analytics#practiceuser
I would like to have the :analytic_id part to be removed. So it will look like this
analytic_practiceday_path POST /analytics/practiceday analytics#practiceday
analytic_practiceuser_path GET /analytics/practiceuser analytics#practiceuser
My current code is
resources :analytics, :only =>"index" do
post 'practiceday', to: "analytics#practiceday"
get 'practiceuser', to: "analytics#practiceuser"
end
I know it is possible to declare it explicitly,
get '/analytic/practiceday', to: "analytics#practiceday", as: "analytic_practice_day"
But I have 20~30 routes to be defined. I would appreciate if anyone can tell me a better way to do it.
Thanks.

How about :
resources :analytics, only: :index do
collection do
post :practiceday
get :practiceuser
end
end

Try
post 'practiceday', to: "analytics#practiceday", on: :collection
Edit: This will reverse the linguistic order of your routes from analytic_practiceday_path to practiceday_analytic_path. I don't know if that's a deal breaker for you. If so, you may be forced to specify each route with :as.

Related

rename rails route

I have a route/path using CcpaAcknowledgmentsController
/ccpa_acknowledgments
I would like the route to be, BUT I would still like it to use the CcpaAcknowledgmentsController
/customers/ccpa_acknowledgments
Since I have these two routes...
resources :customers
resources :ccpa_acknowledgments
match '/customers/ccpa_acknowledgments', to: 'ccpa_acknowledgments#index', via: [:get]
I keep getting a conflict stating
NoMethodError in CustomersController.
Is there a way to get the desired route I want without putting the method/code in the CustomersController?
This is the way to do that
resources :customers do
get :ccpa_acknowledgments, to: 'ccpa_acknowledgments#index', on: :collection
end
Inside the customers block for two reasons:
we are fine with the beginning of the path /customers
we don't want to mess with the other customers' routes. In this way your route inside the block is before the customers default routes and it's not seen as you are calling customers/:id with ccpa_acknowledgments as id because rails takes care of that for you defining that route before the show
Then
get :ccpa_acknowledgments
because we need the second part of the path /ccpa_acknowledgments
to: 'ccpa_acknowledgments#index'
we want to specify the controller and action pair, because we want to use the CcpaAcknowledgmentsController even though we're inside the customers block
on: :collection
because we don't want any :id inside our route. It's a route defined on the customers collection
alternative using resources as asked in the comment. Try
scope :customers do
resources :ccpa_acknowledgments, only: :index
end
but you need to put this before the resources :customers

What's the "Rails way" to route a "diff" between two instances of the same model?

I'm building a "Brand personality" tool that gives you a report based on the text you share on social media.
I have a model PersonalityReport and in routes I have resources :personality_reports.
A new feature is to offer a "diff" between two reports and I'm trying to work out the most "guessable" way to model this in routes.
Ideally I'd like GET /personality_reports/:personality_report_id/diff/:id or something along those lines, and while I could simply put that into routes as a GET route, is there a more Railsy way of specifying a route using the resources / collections style so that my routes.rb file is more easy to understand?
The 'neatest' way can think of is:
resources :personality_reports, param: 'personality_report' do
member do
get 'diff/:id', to: 'personality_reports#action', as: 'diff_route'
end
end
Where obviously to: is your controller#action, and as: is the name of your route. After running rake routes you will see this generates:
diff_route_personality_report GET /personality_reports/:personality_report_id/diff/:id(.:format) personality_reports#action
I think whatever you mentioned is good enough,
resources : personality_reports do
resources :diffs, only: [:show]
end
So, routes like below,
personality_report_diff GET /personality_reports/:personality_report_id/diffs/:id(.:format) diffs#show
NOTE: You can also make diff route in singular resource :diff if you want to make it as singular resource.

How to change namespace URI in Ruby on Rails?

I'm newbie in RoR and I'd like a bit of help here.
I have the following URI:
http://localhost:3000/abouts/2
And the next in Route:
resources :abouts, only: [:show]
I'd like to show the information of "about/2" in another page, for example:
http://localhost:3000/new_about
Regards!
You can simply map the desired URL to your existing action:
get 'new_about', to: 'abouts#show', id: 2
You can add your own routes in rails. You'd do something like :
get "/new_about" => "controller#action"
I would suggest sticking to the Rails way of doing things though unless it's only a few number of pages and you specifically need them to be named differently.

Changing the route from posts/article to blog/article in rails

I'm in trouble with routes in Rails. I've been trying to get a path like: home/blog/title-article but the closes I get is: home/blog/posts/title-article
I've tried with namespace, scope and one by one with get 'blog' => 'posts#blablabla', but I get errors like UrlGenerationError or NameError every time I change the paths. I've read the official guide and some answers in this forum, but I'm getting more confused hehe
In my last try I generated a controller Blog and a scaffold Post. The output of rake routes is: http://i.stack.imgur.com/gdfPc.png
routes.rb
Rails.application.routes.draw do
scope :blog do
resources :posts
end
get 'blog' => 'blog#index'
root 'pages#index'
...
Thank you!
Now my routes are like: http://i.stack.imgur.com/cKsFG.png
Thank you!
Not sure its what you expect but try:
resources :posts, path: 'blog'
Feels weird though.
Btw, I guess you have the errors due to url helpers.
use rake routes to check the existing routes and the related url helpers.
Doc lives here

Custom route yields dot instead of '?id='

I am adding a custon new action for my rails app by adding the following to my routes.rb:
resources :adventures do
member do
match :upvote, via: [:post, :delete]
match :downvote, via: [:post, :delete]
end
get 'seed', on: :new
end
(you can ignore the voting piece, just wanted to show you the whole block)
upvote_adventure POST|DELETE /adventures/:id/upvote(.:format) adventures#upvote
downvote_adventure POST|DELETE /adventures/:id/downvote(.:format) adventures#downvote
seed_new_adventure GET /adventures/new/seed(.:format) adventures#seed
adventures GET /adventures(.:format) adventures#index
POST /adventures(.:format) adventures#create
new_adventure GET /adventures/new(.:format) adventures#new
edit_adventure GET /adventures/:id/edit(.:format) adventures#edit
adventure GET /adventures/:id(.:format) adventures#show
PATCH /adventures/:id(.:format) adventures#update
PUT /adventures/:id(.:format) adventures#update
DELETE /adventures/:id(.:format) adventures#destroy
but this:
seed_new_adventure_path(#adventure_collection.id)
generates this:
http://localhost:3000/adventures/new/seed.6
instead of this:
http://localhost:3000/adventures/new/seed?id=6
I read a lot of posts with people getting dots instead of slashes, but none with adding a an additional new action. Am I doing something wrong, or do I need to add something more?
EDIT: I did make a mistake and did not mean to plurailze the adventure path (Is how I had it originally). The real problem is that all I needed to do was pass the id as a parameter.
Here is the path I was looking for:
redirect_to seed_new_adventure_path(:id => #adventure_collection.id)
It's because you are using the wrong pluralization.
In your example, you are using:
seed_new_adventures_path(#adventure_collection.id)
But the route is properly described as:
seed_new_adventure_path(#adventure_collection.id)
And will probably work fine and be more readable as:
seed_new_adventure_path(#adventure_collection)
Routes
Although Brad Werth is correct (your route pluralization is incorrect), the big problem you have is what you're trying to achieve.
You have specified the following link:
domain.com/adventure/new/seed
This is a get request with no other parameters present. I don't understand why you're passing an object to this route? This is why you're receiving the .6 problem (because Rails cannot build the routes), instead of getting /6
After thinking about what you're trying to do, and I believe you can fix it as follows:
#config/routes.rb
resources :adventures do
...
get "seed(/:id)", on: :new #-> domain.com/adventures/new/seed/6
end
OK, so in order to get this:
http://localhost:3000/adventures/new/seed?id=7
I need to pass a parameter to the link like this:
seed_new_adventure_path(:id => #adventure_collection.id)
I just forgot how to pass parameters!

Resources