I know in rails i can say
resources :articles do
post :something
end
but this would give the regular routes, plus :something, what if i wanted to specify only post :something, or what if wanted to exclude a particular resource, but keep the rest (in my case i am trying to kick out delete)
resources :articles, :except => :destroy do
post :something
end
I think you can just use except
resources :articles, :except => [:destroy]
The other answers are right, but you should check the Guide to Rails Routing (linked you to the section on restricting routes) for more information on what options are available.
Related
I am trying to add followers to my rails application. I am seeing the following error when I run rails s to start my server:
Invalid route name, already in use: 'user' (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here:
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
Routes
Rails.application.routes.draw do
devise_for :users
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
resources :posts do
member do
post '/like' => 'posts#like'
end
end
get ':username' => 'users#show', as: 'user'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
It may be helpful to know that I added:
resources :users do
member do
get :following, :followers
end
end
resources :relationships, only: [:create, :destroy]
Before I added the above, my rails app worked.
I'm sure it's probably a simple fix, but I am new to rails. I've been tinkering with routes and searching online for over an hour, trying to understand and solve the problem. I'm hoping someone more knowledgeable can guide me in the right direction.
Since you are using devise there are some routes already created by devise_for users so avoid using such routes which has conflict with already defined routes. Instead of using as: :user put another relevant name like as::user_profile
Hope it helps
In your case, Devise routes and Users routes are conflicting. You need to change one of them. You can either put Devise under a specific prefix like,
devise_for :users, :path_prefix => 'auth'
resources :users
Reference: https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface
Please do not forget, that there is "as" option too.
devise_for :users, as: 'some_unique_prefix'
Wondering why my /articles route is not coming up when I apply :shallow => true?
routes.rb
resources :users, :shallow => true do
resources :articles
end
Also tried this:
resources :users do
resources :articles, :shallow => true
end
Visiting /articles won't show me all articles from any user as expected, but I can still visit /articles/:id just fine. Is this expected behavior?
The shallow: true option does not provide an index resource according to the docs. Therefore, you will not be able to access just /articles. So yes, this is the expected behavior.
Yes - anything with an ID (show, edit, update, destroy) doesn't need a parent to identify itself.
The other two (create, index), need the parent to scope the query.
You would need to create another resource
resources :articles, :only => [:index]
To see all articles
I am using Rails 3.2
I want to have routing pretty much exactly like github, so:
root/(username)
root/(username)/(projectname)
root/(username)/(projectname)/issus
etc.
I am trying something like this:
resources :publishers do
resources :magazines do
resources :photos
end
end
But that gives routes like this:
/publishers/1/magazines/2/photos/3
A project I am looking at does the following which seems to work but does not seem to be for me.
resources :projects, :constraints => { :id => /[^\/]+/ }, :except => [:new, :create, :index], :path => "/" do
member do
get "team"
get "wall"
get "graph"
get "files"
end
resources :wikis, :only => [:show, :edit, :destroy, :create] do
member do
get "history"
end
end
If you want to get rid of the id number (which is rails default) and use a name I suggest the FriendlyId gem.
watch this railscast http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
and here is the github page https://github.com/norman/friendly_id
EDIT
This is the article I was looking for, I forgot I bookmarked it months ago.
http://jasoncodes.com/posts/rails-3-nested-resource-slugs
You must to use friendly_id and scope
scope '/:username/:projectname', module: 'users/projects', as: 'users_project' do
resources :issus
resources :photos
end
Using this as an example contexted:
Post has_many comments
Comment belongs_to post
I have a route that looks like:
resources :posts do
resources :comments
end
How do i create a route that exposes comments#index?
An example use case would be... I want to list ALL comments in the system on a page. Essentially using the comments resource as if it's not nested when a user hits /comments
thank you!
Try this.
resources :posts do
resources :comments, :except => :index
end
match 'comments' => 'comments#index', :as => :comments
That said, I usually look to avoid routes like this because I like a tidy RESTful routes file, but sometimes it can't be helped.
Second option:
resources :posts do
resources :comments, :except => :index
get :comments, :on => :collection
end
In the second option, you'd want to remove the index action from the comments controller and create a comments action in your posts controller.
In my routes.rb:
resources :posts do
get "test"
end
This produces the usual RESTful routes with /post/:id/.... However, I also get /post/:post_id/test.
Now my problem is that sometimes the parameter is named :id and sometimes it is :post_id. How can I make it uniform?
Thank you!
Specify :on => :member, otherwise it is acting as a nested resource.
resources :posts do
get 'test', :on => :member
end
You shouldn't make it uniform. It's :id when it's the target resource and :post_id when it is the parent of some other target resource (i.e. nested resources). This is a Rails convention.