Creating routes for all the entries in the table rails 4 - ruby-on-rails-4

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.

Related

Customize resource routing in Rails 4

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.

Ruby on rails - removing routes created by resource

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

rails 4 - force two arguments in route

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)

Rails: aliasing the index action in the routes file

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"

Globalize: How to show in the view the current locale information

I am currently having a bit of a problem with Globalize gem.
I explain the current situation:
I have a Model called Question. After creating it, without any data stored, I added the following lines to the model:
class Question < ActiveRecord::Base
translates :wording, :answer1, :answer2, :answer3, :answer4
end
Then, I created a migration to create the translations table
class CreateTranslationsTable < ActiveRecord::Migration
def up
Question.create_translation_table! :wording => :string, :answer1 => :string, :answer2 => :string, :answer3 => :string, :answer4 => :string
def end
def down
Question.drop_translation_table!
def end
My default locale is :en. After that I added some data.
If I go execute rails c and put the command Question.first.wording everything works fine.
Although when I execute in 'rails c' I18n.locale = :es and then I do Question.first.wording still displays the english text I put at the beginning.
I tried one thing which it seemed to help me is that I dropped all the translated columns (like specified in Globalize documentation after you migrated data. In my case I didn't have any data to migrate at the beginning though). After that I made a rollback (which got back the columns I deleted form the Question model), then executing Question.first.wording with I18n.locale = :es got it working. Which means that Question.first.wording returns nil.
After that, I implemented the 'Locale from Url Params' as specified in the Ruby on Rails guide
Which means the first URL param si the ':locale' param.
Now the current problem: The view still displays the information in English when it should display it in Spanish, since the URL I entered was http://localhost.com/es/questions/.
How can I make it to display in the view the Spanish information?
My mistake. I interpreted from the documentation that the the chunck of code (in application_controller.rb) that works for setting the url:
def default_url_options(options={})
{ locale: params[:locale] }
end
would actually set the 'I18n.locale' variable. What I did is the next to get around this (in application_controller.rb):
before_action :change_to_current_locale
def change_to_current_locale
I18n.locale = params[:locale]
end
That made it work.