Ruby on Rails - routing to a new page - ruby-on-rails

I would like to create a new route that leads to the url '../coins/:id/events/pending-events' however my new route is leading to '..coins/:coin_id/events/:event_id/pending-events' when I do this. What am I doing wrong here and how can I fix this?
routes.rb
resources :coins do
...
resources :events do
get 'pending-events', to: 'events#pending'
member do
put "like", to: "events#upvote"
put "dislike", to: "events#downvote"
end
end
...
end
event_controller.rb
...
def pending
#events = Event.where(coin_id: #coin.id).order("created_at DESC")
end
...

Just add on: :collection to your route e.g:
resources :coins do
...
resources :events do
get 'pending-events', to: 'events#pending', on: :collection
member do
put "like", to: "events#upvote"
put "dislike", to: "events#downvote"
end
end
...
end
More info: https://guides.rubyonrails.org/routing.html#adding-collection-routes

I suggest you do:
resources :coins do
resources :events do
collection do
get :pending
end
member do
put "like", to: "events#upvote"
put "dislike", to: "events#downvote"
end
end
end
Which will give you:
pending_coin_events GET /coins/:coin_id/events/pending(.:format) events#pending
like_coin_event PUT /coins/:coin_id/events/:id/like(.:format) events#upvote
dislike_coin_event PUT /coins/:coin_id/events/:id/dislike(.:format) events#downvote
coin_events GET /coins/:coin_id/events(.:format) events#index
POST /coins/:coin_id/events(.:format) events#create
new_coin_event GET /coins/:coin_id/events/new(.:format) events#new
edit_coin_event GET /coins/:coin_id/events/:id/edit(.:format) events#edit
coin_event GET /coins/:coin_id/events/:id(.:format) events#show
PATCH /coins/:coin_id/events/:id(.:format) events#update
PUT /coins/:coin_id/events/:id(.:format) events#update
DELETE /coins/:coin_id/events/:id(.:format) events#destroy
coins GET /coins(.:format) coins#index
POST /coins(.:format) coins#create
new_coin GET /coins/new(.:format) coins#new
edit_coin GET /coins/:id/edit(.:format) coins#edit
coin GET /coins/:id(.:format) coins#show
PATCH /coins/:id(.:format) coins#update
PUT /coins/:id(.:format) coins#update
DELETE /coins/:id(.:format) coins#destroy
No need to specify to: and pending_coin_events_path reads nicely.
Personally, I would do:
resources :coins do
resources :events do
collection do
get :pending
end
member do
put :upvote
put :downvote
end
end
end
Which will give you:
pending_coin_events GET /coins/:coin_id/events/pending(.:format) events#pending
upvote_coin_event PUT /coins/:coin_id/events/:id/upvote(.:format) events#upvote
downvote_coin_event PUT /coins/:coin_id/events/:id/downvote(.:format) events#downvote
coin_events GET /coins/:coin_id/events(.:format) events#index
POST /coins/:coin_id/events(.:format) events#create
new_coin_event GET /coins/:coin_id/events/new(.:format) events#new
edit_coin_event GET /coins/:coin_id/events/:id/edit(.:format) events#edit
coin_event GET /coins/:coin_id/events/:id(.:format) events#show
PATCH /coins/:coin_id/events/:id(.:format) events#update
PUT /coins/:coin_id/events/:id(.:format) events#update
DELETE /coins/:coin_id/events/:id(.:format) events#destroy
coins GET /coins(.:format) coins#index
POST /coins(.:format) coins#create
new_coin GET /coins/new(.:format) coins#new
edit_coin GET /coins/:id/edit(.:format) coins#edit
coin GET /coins/:id(.:format) coins#show
PATCH /coins/:id(.:format) coins#update
PUT /coins/:id(.:format) coins#update
DELETE /coins/:id(.:format) coins#destroy
I like that better because:
You're doing less typing
Your action and paths are parallel
Symbols are (IMO) prettier and subject to fewer typos
But, that's just me.

Related

Rails nested routes not working as expected

I have nested resources in my routes like so. These work perfectly on my other rails 5 app, but not on my rails 6 app. I cannot figure out why it recognizes only the first level of nested stuff.
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end
Here is what rake routes gives me:
blogs_user GET /users/:id/blogs(.:format) users#blogs
like_blog PUT /blogs/:id/like(.:format) blogs#upvote
dislike_blog PUT /blogs/:id/dislike(.:format) blogs#downvote
blog_comments GET /blogs/:blog_id/comments(.:format) comments#index
POST /blogs/:blog_id/comments(.:format) comments#create
new_blog_comment GET /blogs/:blog_id/comments/new(.:format) comments#new
edit_blog_comment GET /blogs/:blog_id/comments/:id/edit(.:format) comments#edit
blog_comment GET /blogs/:blog_id/comments/:id(.:format) comments#show
PATCH /blogs/:blog_id/comments/:id(.:format) comments#update
PUT /blogs/:blog_id/comments/:id(.:format) comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) comments#destroy
PUT /blogs/:id/like(.:format) comments#upvote
PUT /blogs/:id/dislike(.:format) comments#downvote
notations GET /blogs/:id/notations(.:format) notations#index
POST /blogs/:id/notations(.:format) notations#create
new_notation GET /blogs/:id/notations/new(.:format) notations#new
edit_notation GET /blogs/:id/notations/:id/edit(.:format) notations#edit
notation GET /blogs/:id/notations/:id(.:format) notations#show
PATCH /blogs/:id/notations/:id(.:format) notations#update
PUT /blogs/:id/notations/:id(.:format) notations#update
DELETE /blogs/:id/notations/:id(.:format) notations#destroy
On my other app, for example, it would produce
/blogs/:blog_id/comments/:id/like
I make a copy of your routes and replicated in two apps (Rails 5 and Rails 6) and both produced same routes (without three nested level). If you want the /blogs/:blog_id/comments/:id/like route, you must do a small change.
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
end
resources :notations
end
You are missing the "do" "end" block syntax
resources :blogs do
member do
put 'like', to: 'blogs#upvote'
put 'dislike', to: 'blogs#downvote'
end
resources :comments do # here
member do
put 'like', to: 'comments#upvote'
put 'dislike', to: 'comments#downvote'
end
resources :notations
end # and here
end
Anyway, more than two levels of nesting is discouraged by the rails guidelines.

Rails nested resource - setting param: issue

I have the following routes declared:
resources :accounts, param: :account_id do
resources :instructions, param: :instruction_id do
resources :messages, param: :message_id
end
end
So accounts, instructions, messages are the 3 models I have. This gives me the routes:
account_instruction_messages GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format) messages#index
POST /accounts/:account_account_id/instructions/:instruction_instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_account_id/instructions/:instruction_instruction_id/messages/:message_id(.:format) messages#destroy
account_instructions GET /accounts/:account_account_id/instructions(.:format) instructions#index
POST /accounts/:account_account_id/instructions(.:format) instructions#create
new_account_instruction GET /accounts/:account_account_id/instructions/new(.:format) instructions#new
edit_account_instruction GET /accounts/:account_account_id/instructions/:instruction_id/edit(.:format) instructions#edit
account_instruction GET /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#show
PATCH /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#update
PUT /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#update
DELETE /accounts/:account_account_id/instructions/:instruction_id(.:format) instructions#destroy
That looks wrong to me, I was expecting
/accounts/:account_id/instructions/:instruction_id etc...?
Can someone advise what I am doing wrong?
you can do it in next way:
resources :accounts do
resources :instructions do
resources :messages, param: :message_id
end
end
this will make you next routes:
account_instruction_messages GET /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#index
POST /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#destroy
account_instructions GET /accounts/:account_id/instructions(.:format) instructions#index
POST /accounts/:account_id/instructions(.:format) instructions#create
new_account_instruction GET /accounts/:account_id/instructions/new(.:format) instructions#new
edit_account_instruction GET /accounts/:account_id/instructions/:id/edit(.:format) instructions#edit
account_instruction GET /accounts/:account_id/instructions/:id(.:format) instructions#show
PATCH /accounts/:account_id/instructions/:id(.:format) instructions#update
PUT /accounts/:account_id/instructions/:id(.:format) instructions#update
DELETE /accounts/:account_id/instructions/:id(.:format) instructions#destroy
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:id/edit(.:format) accounts#edit
account GET /accounts/:id(.:format) accounts#show
PATCH /accounts/:id(.:format) accounts#update
PUT /accounts/:id(.:format) accounts#update
DELETE /accounts/:id(.:format) accounts#destroy
UPDATE
but if you need all same params, you can do:
resources :accounts, only: [] do
resources :instructions, only: [] do
resources :messages, param: :message_id
end
end
resources :accounts, param: :account_id
resources :instructions, param: :instruction_id
and this will return:
account_instruction_messages GET /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#index
POST /accounts/:account_id/instructions/:instruction_id/messages(.:format) messages#create
new_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/new(.:format) messages#new
edit_account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id/edit(.:format) messages#edit
account_instruction_message GET /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#show
PATCH /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
PUT /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#update
DELETE /accounts/:account_id/instructions/:instruction_id/messages/:message_id(.:format) messages#destroy
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:accounts_id/edit(.:format) accounts#edit
account GET /accounts/:accounts_id(.:format) accounts#show
PATCH /accounts/:accounts_id(.:format) accounts#update
PUT /accounts/:accounts_id(.:format) accounts#update
DELETE /accounts/:accounts_id(.:format) accounts#destroy
....... and so on

Restful url not picking up id

I am having troubles with routes. I have defined routes like this:
resource :demo
resource :subjects
resource :pages
resource :sections
when i do
rake routes
it doesn't show right urls. it shows something like
Prefix Verb URI Pattern Controller#Action
root GET / demo#index
demo POST /demo(.:format) demos#create
new_demo GET /demo/new(.:format) demos#new
edit_demo GET /demo/edit(.:format) demos#edit
GET /demo(.:format) demos#show
PATCH /demo(.:format) demos#update
PUT /demo(.:format) demos#update
DELETE /demo(.:format) demos#destroy
subjects POST /subjects(.:format) subjects#create
new_subjects GET /subjects/new(.:format) subjects#new
edit_subjects GET /subjects/edit(.:format) subjects#edit
GET /subjects(.:format) subjects#show
PATCH /subjects(.:format) subjects#update
PUT /subjects(.:format) subjects#update
DELETE /subjects(.:format) subjects#destroy
pages POST /pages(.:format) pages#create
new_pages GET /pages/new(.:format) pages#new
edit_pages GET /pages/edit(.:format) pages#edit
GET /pages(.:format) pages#show
PATCH /pages(.:format) pages#update
PUT /pages(.:format) pages#update
DELETE /pages(.:format) pages#destroy
sections POST /sections(.:format) sections#create
new_sections GET /sections/new(.:format) sections#new
edit_sections GET /sections/edit(.:format) sections#edit
GET /sections(.:format) sections#show
PATCH /sections(.:format) sections#update
PUT /sections(.:format) sections#update
DELETE /sections(.:format) sections#destroy
GET /:controller(/:action(/:id(.:format))) :controller#:action
none of the urls has :id in them. what i might be doing wrong? It still sends id to controller but i am having hard time calling index and show methods as both of them are mapped to -----#show
This is because you are using singular resource, e.g. resource :foo. When you use singular resource, you don't get the :id. In order to get the :id in the parameter, you should change the resources declarations to plural resources:
resources :demoes
resources :subjects
resources :pages
resources :sections

Username in urls for nested routes

I'm a new rails developer making an application where I have a user profile like: localhost:3000/posts routing to localhost:3000/Charles where Charles is the username.
So the code I'm using to do this looks like:
routes.rb
match "/:username" => "posts#index"
and then in my controller:
#user = User.find_by_username params[:username]
#search = Post.search do |query|
query.fulltext params[:search]
query.with(:user, #user.id)
end
this works great just for the post index, but I'd like to have posts/18 (for example) be routed to /username/posts/18.
So basically, I'm asking if there's a way to do something like this:
match "/:username" => "posts#index" do
resources :posts
end
Thanks for all help!
This
scope '/:username' do
resources :posts
end
produces what you want:
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PATCH /:username/posts/:id(.:format) posts#update
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy
Try
scope ':username' do
resources :posts
end
bundle exec rake routes
posts GET /:username/posts(.:format) posts#index
POST /:username/posts(.:format) posts#create
new_post GET /:username/posts/new(.:format) posts#new
edit_post GET /:username/posts/:id/edit(.:format) posts#edit
post GET /:username/posts/:id(.:format) posts#show
PUT /:username/posts/:id(.:format) posts#update
DELETE /:username/posts/:id(.:format) posts#destroy
match "/:username/posts/:id" => "posts#show"
But that won't really help when you need to edits and such. So, try:
scope "/:username" do
resources :posts
end
That failing:
scope "/:username" do
get '' => "posts#index"
get 'posts/:id' => "posts#show"
end
Hopefully the first will work. Haven't tried it myself so forgive me, but I'm pretty sure that, in general, scope is what you want.

Nested routes work but others don't

Struggling with routing in Rails!
This works: http://127.0.0.1:3000/locations/1/statistics but http://127.0.0.1:3000/locations/ does not work.
My routes look like this:
resources :locations do
resources :statistics
end
I can get only http://127.0.0.1:3000/locations/ working if I just do
resources locations
but then the nested routes don't work!
How can I get both working?
Many thanks.
EDIT rake routes:
location_statistics GET /locations/:location_id/statistics(.:format) statistics#index
POST /locations/:location_id/statistics(.:format) statistics#create
new_location_statistic GET /locations/:location_id/statistics/new(.:format) statistics#new
edit_location_statistic GET /locations/:location_id/statistics/:id/edit(.:format) statistics#edit
location_statistic GET /locations/:location_id/statistics/:id(.:format) statistics#show
PUT /locations/:location_id/statistics/:id(.:format) statistics#update
DELETE /locations/:location_id/statistics/:id(.:format) statistics#destroy
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:id/edit(.:format) locations#edit
location GET /locations/:id(.:format) locations#show
PUT /locations/:id(.:format) locations#update
DELETE /locations/:id(.:format) locations#destroy
home_index GET /home/index(.:format) home#index
about /about(.:format) home#about
contact /contact(.:format) home#contact
root / home#index
EDIT 2 routes file
match '/about/' => 'home#about'
match '/contact/' => 'home#contact'
resources :locations do
resources :statistics
end
get "home/index"
EDIT 3
My actual error:
Routing Error
No route matches {:controller=>"statistics", :location_id=>nil}
when I go to http://127.0.0.1:3000/locations/
You should either use
=link_to "Locations", locations_path
or
# get sure #location is not nil
=link_to "Location Statistics", location_statistics_path(#location)

Resources