I have a category model and in my routes.rb, I have
resources :categories
which generates the following set of routes.
categories_path GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category_path GET /categories/new(.:format) categories#new
edit_category_path GET /categories/:id/edit(.:format) categories#edit
category_path GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
Now, what I need is except for all GET routes, I want the rest of the routes to be under '/admin' scope. So that operations like create, edit and delete are accessed at admin/categories/:id/edit etc.
Is there an easy way to mention this scope?
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an admin namespace. You would place these controllers under the app/controllers/admin directory, and you can group them together in your router:
namespace "admin" do
resources :posts, :comments
end
This will create a number of routes for each of the posts and comments controller. For Admin::PostsController, Rails will create:
GET /admin/posts
GET /admin/posts/new
POST /admin/posts
GET /admin/posts/1
GET /admin/posts/1/edit
PATCH/PUT /admin/posts/1
DELETE /admin/posts/1
check the rest of it through the apidock documentation
I think you can define route of categories twice.
resources :categories, :only => :index
resources :categories, :except => :index, :path => 'admin/categories'
Related
I have an application with an object, movies, that doesn't use some of the standard RESTful routes. I don't want the 'new' route to lead anywhere.
The problem is I have 'movies' with a nested resource 'reviews'
resources :movies do
resources :reviews
end
I want this style of routing:
get '/movies', to: "movies#index"
But with nested routes. Is this possible? I'm sure there's answer to this somewhere on this site, but I can't find it.
You can simply do:
resources :movies, :only => [:index] do
resources :reviews
end
Which will give you:
movie_reviews GET /movies/:movie_id/reviews(.:format) reviews#index
POST /movies/:movie_id/reviews(.:format) reviews#create
new_movie_review GET /movies/:movie_id/reviews/new(.:format) reviews#new
edit_movie_review GET /movies/:movie_id/reviews/:id/edit(.:format) reviews#edit
movie_review GET /movies/:movie_id/reviews/:id(.:format) reviews#show
PATCH /movies/:movie_id/reviews/:id(.:format) reviews#update
PUT /movies/:movie_id/reviews/:id(.:format) reviews#update
DELETE /movies/:movie_id/reviews/:id(.:format) reviews#destroy
movies GET /movies(.:format) movies#index
I'm taking an MOOC and the goal of this exercise is to add a new functionality to typo, where i can merge two articles together.
When I add the route to my new function merge to the routes.rb I'm losing the functionality to delete articles. I think something clashes here, but I have no idea what.
The original routes.rb:
%w{advanced cache categories comments content profiles feedback general pages
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i|
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false
end
This method in articles.rb creates the correct url for deleting
def delete_url
blog.url_for(:controller => "/admin/content", :action =>"destroy",:id => id)
end
correct url:
http://example.com/admin/content/destroy/7
If i follow this link i can successfully delete an article.
However, if I add the following before that to my routes.rb:
namespace "admin" do
resources :content do
post :merge, on: :member, as: :merge
end
end
The new merging functionality and forms are working fine, but the method delete_url now produces something like this:
http://example.com/admin/content/7
and if I follow a link created by this method i get:
Unknown action
The action 'show' could not be found for Admin::ContentController
Maybe I'm overwriting something? I can't figure out what's happening here and why this affects the delete action / route.
Thanks in advance!
EDIT: rake routes | grep content:
with the original routes.rb gives me:
admin_content /admin/content {:controller=>"admin/content", :action=>"index"}
/admin/content(/:action(/:id)) {:action=>nil, :id=>nil, :controller=>"admin/content"}
whereas my modified routes.rb produces
merge_admin_content POST /admin/content/:id/merge(.:format) {:action=>"merge", :controller=>"admin/content"}
admin_content_index GET /admin/content(.:format) {:action=>"index", :controller=>"admin/content"}
POST /admin/content(.:format) {:action=>"create", :controller=>"admin/content"}
new_admin_content GET /admin/content/new(.:format) {:action=>"new", :controller=>"admin/content"}
edit_admin_content GET /admin/content/:id/edit(.:format) {:action=>"edit", :controller=>"admin/content"}
admin_content GET /admin/content/:id(.:format) {:action=>"show", :controller=>"admin/content"}
PUT /admin/content/:id(.:format) {:action=>"update", :controller=>"admin/content"}
DELETE /admin/content/:id(.:format) {:action=>"destroy", :controller=>"admin/content"}
/admin/content {:controller=>"admin/content", :action=>"index"}
/admin/content(/:action(/:id)) {:action=>nil, :id=>nil, :controller=>"admin/content"}
Okay, thanks to #guitarman i worked through my routes code and found out I can add the following except:
namespace "admin" do
resources :content, except: [:index, :show, :update, :destroy, :edit, :new, :create] do
post :merge, on: :member, as: :merge
end
end
after that, the rake routes just shows the additional merge route I wanted and my destroy action works fine again.
Check rake routes command. I think there is a route /admin/content/:id which will be created by resources :content in the namespace "admin".
Your request to http://example.com/admin/content/7 will be catched be the defined route but I gess you have no show action in the controller.
Better:
namespace "admin" do
post "/content/:id/merge", to: "admin/content#merge", as: :merge
end
For more information about recources and the CRUD operations please see the rails routing guide.
in my routes.rb i have foll entries:-
resources :products do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
i want to change every /products to /eshop in the url.
i am not sure but can i use :path=>:eshop.Will it will be also applicable to the sub routes as well such as eshop/get_products,eshop/buy_this...etc.
You can modify your routes and run rake routes in the terminal to check the paths.
resources :products, :path => 'eshop', :as => 'eshop' do
get 'get_products', :on => :collection
get 'show_product_details',:on=>:member
get 'buy_this',:on=>:collection
get 'search_product',:on=>:collection
end
will produce these
get_products_eshop_index GET /eshop/get_products(.:format) products#get_products
show_product_details_eshop GET /eshop/:id/show_product_details(.:format) products#show_product_details
buy_this_eshop_index GET /eshop/buy_this(.:format) products#buy_this
search_product_eshop_index GET /eshop/search_product(.:format) products#search_product
eshop_index GET /eshop(.:format) products#index
POST /eshop(.:format) products#create
new_eshop GET /eshop/new(.:format) products#new
edit_eshop GET /eshop/:id/edit(.:format) products#edit
eshop GET /eshop/:id(.:format) products#show
PUT /eshop/:id(.:format) products#update
DELETE /eshop/:id(.:format) products#destroy
Edit: I've seen a number of these but couldn't find an answer to this so I'm attempting to document it as best I can and asking this question.
I have a model-less rails app (calling an API) with a nested comments resource. I am able to post a comment against a story if I go directly to the comments#new or comments#index action and accordingly post to the comments#create action.
However I'd like very much to be able to post a comment on the same page as the #show action of the parent resource: (opusses#show)
I've tried using the rails url_helper path from rake routes as opuss_comments_path and explicitly stating the controller and action. In both cases I still get this message:
No route matches {:controller=>"comments", :action=>"create"}
Here is my routes db:
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :osessions, only: [:new, :create, :destroy]
resources :authors do
member do
get :following
get :followed
post :follow
end
end
resources :opusses do
resources :comments
member do
get :like
get :authorfeed
post :repost
end
end
And my Rake Routes:
DELETE /authors/:id(.:format) authors#destroy
opuss_comments GET /opusses/:opuss_id/comments(.:format) comments#index
POST /opusses/:opuss_id/comments(.:format) comments#create
new_opuss_comment GET /opusses/:opuss_id/comments/new(.:format) comments#new
edit_opuss_comment GET /opusses/:opuss_id/comments/:id/edit(.:format) comments#edit
opuss_comment GET /opusses/:opuss_id/comments/:id(.:format) comments#show
PUT /opusses/:opuss_id/comments/:id(.:format) comments#update
DELETE /opusses/:opuss_id/comments/:id(.:format) comments#destroy
&&
like_opuss GET /opusses/:id/like(.:format) opusses#like
authorfeed_opuss GET /opusses/:id/authorfeed(.:format) opusses#authorfeed
repost_opuss POST /opusses/:id/repost(.:format) opusses#repost
opusses GET /opusses(.:format) opusses#index
POST /opusses(.:format) opusses#create
new_opuss GET /opusses/new(.:format) opusses#new
edit_opuss GET /opusses/:id/edit(.:format) opusses#edit
opuss GET /opusses/:id(.:format) opusses#show
PUT /opusses/:id(.:format) opusses#update
DELETE /opusses/:id(.:format) opusses#destroy
When I call the code below from comments#index page it works perfectly. However it's quite common to post to another form from a different controller and when I call this code from the opusses#show page it fails with the error above.
On the off chance it had to do with the URL helper, I tried specifying the controller and action explicitly and that still didn't work - generated the same error.
Classic newbie mistake, but for others benefit =>
I had rake routes and I had the path correct, what I wasn't doing was submitting a the id of the parent resource. So POST to the path and include the object in question. In my case this mean opuss_comments_path(#opuss["xyz"]) where xyz was the id of my object.
opuss_comments GET /opusses/:opuss_id/comments(.:format) comments#index
POST /opusses/:opuss_id/comments(.:format) comments#create
Ah.. learning. :)
Based on your routes, You shouldn't have to use a url helper. but you do have to make sure that you have a handle on the Opuss object in the controller. so do something like this ;
#opuss = Opuss.find(params[:id]) #or your equivalent finder code
#comment = #opuss.comments.build
and then in your view;
<%= form_for([#opuss, #comment]) do |f| %>
.... rest of form
<% end %>
I develop a Rails application and I added simple route:
Name::Application.routes.draw do
resource :categories
end
The problem is that there is no member id in the generated URLs:
$ rake routes
categories POST /categories(.:format) categories#create
new_categories GET /categories/new(.:format) categories#new
edit_categories GET /categories/edit(.:format) categories#edit
GET /categories(.:format) categories#show
PUT /categories(.:format) categories#update
DELETE /categories(.:format) categories#destroy
I use Rails 3.2.3. I don't use ActiveRecord in the application (but I don't know if it is relevant). I have a model Category and CategoriesController.
What might be the problem?
You forgot to add s in the end:
resources :categories
resources and resource are different things: resources and resource.