Adding a restful route like: /users/234/add_section - ruby-on-rails

I already have:
resources :users
How can I add a route (for POST only) that will work like:
/users/2343/add_section
Is it possible to add this route inside of the resources users block?
resources :users do
end

Having add_section there is not very RESTful. Consider having a subresource section and doing POST "/users/123/sections/"

Something like:
resources :users do
member do
post 'add_section'
end
end

As I answered on your previous question
resources :users do
post :add_section, :on => :member
end

Related

No route matches [PUT], resource and method are defined though

I'm using postman to make a PUT or PATCH request, but it says No route matches [PUT] "/api/registrations"
my URL looks like this
http://localhost:3000/api/registrations?id=5&status=approved"
My routes.rb file:
Rails.application.routes.draw do
scope :api do
resources :professors
resources :registrations
resources :schedules
resources :notifications
resources :users
resources :meetings
resources :courses
resources :students
end
end
I have a defined update method in my RegistrationsController and My POST and GET routes work.
The URL you're using is incorrent. You should not pass id in query, but in path.
A correct URL is
http://localhost:3000/api/registrations/5?status=approved
Rails sets id as last element of a resourceful route.
Docs say:
resources :photos
(...)
PATCH/PUT /photos/:id photos#update update a specific photo
Run rails routes on the command line to get the proper URL pattern and it's matching Controller action.

Custom route redirect to wrong action

I have the following in my routes file:
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run, to: 'test_cases#run'
end
end
end
end
get 'test_cases/test', to: 'test_cases#test'
My problem is with the test route, outside the resources. Checking the available routes I have what I want:
test_cases_test GET /test_cases/test(.:format) test_cases#test
However, if I call test_cases_test_url from a view I'm redirected to test_cases#show, no test_cases#test how it should be. This question is about the same problem in a different situation. I don't want to follow the accepted answer because if I put get 'test', to: 'test_cases#test' inside my resources :test_cases and outside member block I'll got the question_id in my route, and inside member block the test_case_id. I don't need these ids.
I have any option to get my desired route(test_cases/test) working?
i think you're matching on the test_cases#show method because it has the same pattern as the test_cases/test url and it comes before. Just move the test_cases/test get route to the top of your routes file
get 'test_cases/test', to: 'test_cases#test'
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run, to: 'test_cases#run'
end
end
end
end
Another way, which I'd recommended
resources :exercises, shallow: true do
resources :questions do
resources :test_cases do
member do
post :run
get :test
end
end
end
end

Rails how to make example.com/post/1 to example.com/blog/post/1

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

Setting up routes in rails

When I put the following code in my routes config :
resources :users do
end
I get all the CRUD operations routes. i.e
/users/new
/users/:id/edit
and so on.
How do I configure routes so I get route like this :
/users/lookup/:search_query
And when users reaches this routes he/she should be taked to lookup method of my controller
I would do :
resources :users do
get :lookup, on: :collection
end
And I would pass the search_query as a parameter. With that you will be more flexible.
resources :users do
get '/lookup/:search_query' => 'users#lookup', on: :collection
end
resources :users do
end
match '/users/lookup/:search_query' => "users#lookup", :as => :user_lookup

Rails: How to set params on some paths with scope and set them to nil on some others?

I'm using some routes with scopes, and some without. See below.
resources :cities
resources :categories
devise_for :clients
namespace :clients do
resources :account
resources :dashboard
resources :offers
end
scope "/:current_city" do
scope "/:current_category" do
match 'articles/last_articles' => 'articles#index_last_articles', :as => "last_articles"
resources :articles do
resources :comments
end
end
end
root :to => "home#index"
I am using that params :current_city and :current_category and it gives me a URL like
http://localhost:3000/warszawa/all/articles/last_articles when I'm accessing articles.
**PROBLEM**
I have now such a problem that if I click on a link_to cities_path or root_path, then it adds those two parameters to the URL as http://localhost:3000/?current_category=all&current_city=warszawa.
I don't want these two parameters destroying the beauty of my URL :o(
The only way I found was to pass :current_city => nil, :current_category => nil for each link, but that's really heavy. I tried also the same but in my routes, which is working for normal resources, but not for namespaces, root_path, devise_for routes, and to be honest, that looks horrible in the routes.rb.
**QUESTIONS**
First I do not understand why these params are passed everywhere if I ask them only in the section with scope?!
Secondly, is there a way to make it work like I want or I should modify my routes?
I wish you understand my problem and if you have any comment or idea, please do not hesitate!
Thx

Resources