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)
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.
I have the following url:
http://example.com/user/login
If I input the url in the browser, it matches de rule:
'<module:user>/<slug:[\w\-]+>' => '<module>/<slug>'
If I create the url:
Yii::$app->urlManager->createAbsoluteUrl(["user/index", "slug" => "login"]);
It should create the same url as above but instead it creates:
http://example.com/user/index?slug=login
But If I change the rule to:
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
It works ok, any ideas why? I guess for some reason:
It is passing slug empty to or
It is an invalid value.
Any ideas?
That is because slug is part of the route pattern: '<module>/<slug>'. So <slug:[\w\-]+> is not treated as a named GET param, but as a part of the route. That means that URL /user/something will point to route user/something without any GET params.
You should not use the same name for route patterns and named params. You should either use different name:
'<module:user>/<action:[\w\-]+>/<slug:[\w\-]+>' => '<module>/<action>'
Or hardcode action for specified rule (as you already did in second example):
'<module:user>/<slug:[\w\-]+>' => '<module>/index'
Note that this will also match standard actions, like user/view.
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 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"