I have the following route set up. I need to make the index action automatically use the pubmed_search route below.
resources :users do
resources :publications do
collection do
get :pubmed_search
post :pubmed_list
end
end
end
I tried
resources :users do
resources :publications do
collection do
get 'publications', :action => :pubmed_search
get :pubmed_search
post :pubmed_list
end
end
end
Without luck I could just do a redirect in the index method of the controller but i am sure there is a Rails way to do this and I want to learn.
EDIT:
This works
get "/users/:user_id/publications" => "publications#pubmed_search", :as => "user_publications"
But isn't there a better way, using the RESTful resources?
This works
get "/users/:user_id/publications" => "publications#pubmed_search", :as => "user_publications"
Related
I have a table called Article
which includes column as art_url which is list of url that i want to create route for each entry.
routes.rb looks like
resources :articles, only: [], path: '' do
collection do
constraints RestrictedListConstraint.new do
get '/:art_url', to: 'articles#show', :as => :show_content
end
end
end
restricted_list_contraint.rb looks lk
class RestrictedListConstraint
def initialize()
#ips = Article.pluck(:blog_url)
end
def matches?(request)
#ips.include?(request)
end
end
getting No route matches [GET] "/how-to-promote"
note:'how-to-promote' is entry in article table.
if i remove entire constraint concept this works fine but none of the routes below this is not getting recognised and all are redirecting to show_content method.
may be because '/:art_url' taking all string and redirecting to show_content
Any help would be appreciated.
I dont want to
get 'articles/:art_url', to: 'articles#show', :as => :show_content
it must be
get '/:art_url', to: 'articles#show', :as => :show_content
and art_url should be articles column art_url entries.
think of the resource routing of a photo-class for example.
If I this to my routes.rb I will get following routes:
resources :photos
# GET '/photos/', :to => 'photos#index'
# GET '/photos/:photo_id/, :to => 'photos#show'
# and so on and so on
Now what I want is to replace the word /photos in all the routes with a simple /p so that I can get a short URL like /p/1 for the first photo. Is there a way to simply alter the resource-line or do I have to manually add each route?
This will make all your routes via :photos through p
resources :p, :controller => "photos"
To be more concise and avoid the issue with p_id, you could do it like this :
resources :photos, path: 'p'
This way, you benefits from the readibility on your end (it will generate helpers like edit_photo_path, you will access variables as photo_id in case of a nested route and such) and generate the named URLs you do want.
I have the following in my routes file
resources :users do
resource :question
end
which creates a /users/:user_id/question route to question#show among other ones. However instead of a URL which has user id in it to show every question the particular user created, I want a URL to show details of one specific question. Something like /question/:id
So to do this I added the line below to my routes file
resources :questions, param: :question_id
which generated a list of routes
questions_path GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question_path GET /questions/new(.:format) questions#new
edit_question_path GET /questions/:id/edit(.:format) questions#edit
question_path GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format)
This list produces the /questions/:id path but unfortunately with the same questions#show connection which collides with the previous one. So I removed the "resources :questions, param: :question" and added the following
get 'questions/:id', :to => 'questions#show_question'
This produces the appropriate route but for some reason doesn't get rid of routes created from "resources :questions, param: :question" even after restarting the server. So every time i visit /question/:id it's going to question#show instead of question#show_question
How do i get rid of the effect of "resources :questions, param: :question" even after having removed it from the routes.rb file?
I would remove the route get 'questions/:id', :to => 'questions#show_question'
and use shallow routes instead
resources :users do
resources :questions, shallow: true
end
And then your url_helper should be
<td><%= link_to 'Show', question_path(question) %></td>
This allows URLs for resources that otherwise would be deeply nested such as a comment on a blog post like /users/a-long-permalink/comments/1234 to be shortened to just /comments/1234.
More info on shallow routes here http://edgeguides.rubyonrails.org/routing.html
I would like to create url in 'routes.rb'.
I've got a resource 'Source', and source_controller that has 'index' method.
In order to reach 'index', I want the user to insert an url with two arguments like that:
source/{type}/{ids}/index
The code I wrote in routes works:
resources :sources, param: :types, :only => :index do
member do
get ':ids/index' => 'source#index'
end
end
It works, but is there a way to do it in more elegant way? (adding the ids to the resource somehow)
I have API engine inside my Rails app and I've mounted the engine under in the main app routes
Rails.application.routes.draw do
mount Api::Engine => "/api"
end
and I want to add the doorkeeper routes using use_doorkeeper function in my routes like this
Api::Engine.routes.draw do
use_doorkeeper :scope => "api/oauth"
end
this doesn't work because it tries to find controllers under api/doorkeeper/controller_name instead of doorkeeper/controller_name
as a workaround I've added doorkeeper routes in main app routes.rb with a scope like this
Rails.application.routes.draw do
mount Api::Engine => "/api"
use_doorkeeper :scope => "api/oauth"
end
But I want to know if there is a solution so I can still add the routes to api/config/routes.rb and make reference the correct controllers path
my colleague suggested this solution found here and it worked for me :)
MyEngine::Engine.routes.draw do
old_scope = #scope[:module]
#scope[:module] = nil
use_doorkeeper
#scope[:module] = old_scope
end