I wanted to do like http://domain:3000/users/id/edit/details. I've already create a new action under app>views>users>details.html.erb. Also in UsersController i put:
def details
....
end
when I run rake routes, I got missing Helper Path/Url on the left side for GET, /users/:id/edit/details(.:format), Users#Details.
Also in routes.rb, I've:
resources :users
I try to insert:
match "users/:id/edit/details", to: 'users#details', via: 'get'
but stil didnt solve my problem. Please help me!
In routes, you'd want to do this:
resources :users do
get :details
end
In this, you'd have the advance form. The url for this will be
/users/:id/details/
Check rake routes to see what's in there.
You may need another update method (the form post action) to handle the advanced form
What you are looking for is called nested resources
resources :users do
resources :details
end
Take a look at http://guides.rubyonrails.org/routing.html#nested-resources for more info
Related
I have created an extra route called 'add' in my Groups controller.
get 'groups/add', to: 'groups#add'
My goal is to have the following link http://localhost:3000/groups/4/add
I thought about doing this nested route
resources :groups do
get 'groups/add', to: 'groups#add'
end
However when I run rake routes it is not listed. I have never really worked much with 'custom' routes before. I typically make due with the ones generated by a scaffold. How can I achieve this?
The following nested route has fixed my issue:
resources :groups do
get 'add', on: :member
end
I have a blog with root
root 'posts#index'
And works best with example.com/ to example.com/posts
But what I want is something like this:
example.com/blog/posts/1.
I've tried creating blog Controller and add
resources :blog do
resources :posts
end
But this is making my routes to blog/:id/posts/:id
If you don't have the relationship between the post and the blog as you mentioned, rails gives you the freedom to declare routes as our own.
so, to make the route example.com/posts/1 to, example.com/blog/posts/1, just add a custom route at the last.
get '/blog/posts/:id', to: :show, controller: 'posts'
what this does is over rides the previous route and make this route final.
Now type rake routes and it will give the last route for you as,
GET /blog/posts/:id(.:format) posts#show
Now you can access using,
example.com/blog/posts/1
Reference for rails routing
Just to expand upon #Sravan answer. If you have multiple routes that will start with /blog/ you might want to check Rails guide on routing.
You can add something along the lines of
scope '/blog' do
resources :posts
resources :users
resources :images
end
Which will create corresponding routes under /blog/.
namespace :blog do
resources :posts
resources :users
resources :images
end
And your controller with namespace will look like this: Blog::PostsController
I have an AppleController
it has a def sliceme method
when I go to: /apple#sliceme
it routes to #index
In my routes.config I have
resources :apples
Why?? And what is the correct route??
Resources will create the CRUD method routes (see here)
If you want to specificity another route you can specify it like so in your routes file:
get "apple/sliceme", to: "apple#sliceme"
Or
resources :apple do
get :sliceme, on: :collection
end
To check what routes actually exist, run rake routes in the terminal
Normally I would expect resources to set the routes where
/groups/:id
However, when I call rake routes I get
GET /groups(.:format) groups#show
Also, whenever I use the view helper method groups_path(group.id) it provides the url link as
/groups.1
Finally in my routing file
resource :groups
This is the case for edit, update, and destroy actions as well. Finally, it doesn't produce an index action. I have to create that after I write the resources method. What's wrong and how do I fix this?
Pluralize that resource:
resources :groups
resources :groups
instead of
resource :groups
I am learning rails and routing has me wanting to jump off the roof.
I am confused on how to go about routing my activation at this point.
I have the following currently in my user routing:
resources :users, only: [:new,:create,:show]. Now I want a route to Users#activate like this www.app.com/users/activate/:a_long_token. Now I know I can just simply do a match '/activate/:auth_token', to: 'users#activate but I am not sure whether this is convention. I was reading this guide on user authentication but it seems its routing is rails 2. Can I do something to add the route mentioned above by simply adding something to the resource itself. By that I mean doing something like (I know this won't work)
resource :users do
member do
get :activate
end
end
rails3 guide
http://guides.rubyonrails.org/
http://guides.rubyonrails.org/routing.html
resources :users do
collection do
get "activate/:a_long_token" => "users#activate", as: :activate
end
end
rake routes outputs this
activate_users GET /users/activate/:a_long_token(.:format) users#activate