Dynamic Routes Settings In Ruby on Rails Like Facebook - ruby-on-rails

Current routes has been defined as:
Rails.application.routes.draw do
namespace :users do
resources :mapps
resources :listings
resources :likes
get 'followers' => 'connections#followers'
get 'following' => 'connections#following'
post 'unfollow' => 'connections#unfollow'
end
get ':username' => 'users#public_profile'
end
I would like make routes like facebook:
:username/:controller/:action => users/:controller/:action
For example, If user hit a URL as /myusername/posts/12 then request must goes to controller file inside user folder & User:Posts
I have seen many related questions but did not work with Rails 4.2.3

An example from http://guides.rubyonrails.org/routing.html#prefixing-the-named-route-helpers
scope ':username' do
resources :mapps
resources :listings
resources :likes
get 'followers' => 'connections#followers'
get 'following' => 'connections#following'
post 'unfollow' => 'connections#unfollow'
end

Related

rails4 ckeditor (No route matches [POST] "/ckeditor/

I have installed Rails4 (rails (4.1.1))
I Installed Ckeditor+paperclip (http://rubydoc.info/gems/ckeditor/4.0.11/frames)
But if I try to upload images, I get this error in log:
ActionController::RoutingError (No route matches [POST] "/ckeditor/EDITOR.config.filebrowserImageUploadUrl"):
i need help please! This was worked In rails 4.0.0.
My route list
... mount Ckeditor::Engine => '/ckeditor'
I use Ckeditor with Paperclip
My config/routes.rb
Rails.application.routes.draw do
resources :binaries
resources :uploads
resources :coms
resources :comments
mount Ckeditor::Engine => '/ckeditor/'
resources :parts
get "/parts/page/:id" => "parts#page"
resources :answer_types
resources :from_sender_msgs
resources :ufknews
get "/general" => "ufknews#general"
get 'ufk13/ufk13pol'
get 'ufk13/governance'
get 'ufk13/contacts'
get "errors/error_404"
get "errors/error_403"
devise_for :users
devise_scope :user do
get "sign_in" => "devise/sessions#new"
get "logout" => "devise/sessions#destroy"
delete "logout" => "devise/sessions#destroy"
get "users" => "users#index"
get "users/:id" => "users#show" , as: :user_root, as: :user
get "users/:id/edit" => "users#edit" , as: :user_edit
get "users/delete" => "users#delete"
put "users" => "users#update"
end
get 'persons/profile'
resources :budget_types
resources :positions
resources :message_types
resources :message_states
resources :messages
resources :senders
resources :organisations
root 'ufknews#general'
get 'home/index'
get 'home/about'
get 'home/contacts'
get 'home/dufk'
get 'home/photo'
get "/*other" => redirect("/errors/error_404")
end
So, you can modify your routes to use POST request instead PUT while try to update data with ckeditor.
For example, if you have in your routes.rb:
resources :pages
you can update this to:
resources :pages, :except => ['update']
post 'pages/:id' => 'pages#update'
It's all!
This method kill a two rabbits:
Modify route for ckeditor;
Improve your project to update big fields over header size limits on your backend of client browser.

custom route for show action using slugs

I'm attempting to create some urls for a model that I want display. I have articles, which belong to sections which belong to issues.
I would like my URLs for the article show action to look like this:
/issue-slug/section-slug/article-slug issues articles and sections have slugs that are stored in the db.
Right now I have a backend section called 'pressroom' and I have the following routes for that. Here is the whole routes.rb file
MaskmagazineCom::Application.routes.draw do
devise_for :users, :path_names => { :sign_up => "register"}, :controllers => { :registrations => "registrations" }
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
root 'magazine#index'
get 'users/' => 'users#index'
# Lobby Routes
# /log-in
devise_scope :user do
get '/sign-in' => 'devise/sessions#new'
end
# /subscribe
get 'subscribe' => 'subscribe#stepone'
get 'subscribe/sliding-scale' => 'subscribe#steptwo'
get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'
# Pressroom Routes
get '/pressroom' => 'pressroom#index'
scope 'pressroom' do
resources :issues, :articles, :sections, :users, :authors
end
How can I pull out the show action and route it to the url that I described?
EDITED:
I've come up with what i want it to do in the routes file, but i need the corresponding controller code:
get '/:issue_slug/:section_slug/:article_slug' => 'article#show'
I'd recommend checking out friendly_id for the slugs.
for the routes, you'll want your routes to read:
# Mag Routes
get '/mag' => 'mag#index' #or wherever you're headed
scope 'mag' do
resources :issues do
resources :sections do
resources :articles
end
end
end
end

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 3 routes for single controller and multiple resources

I have multiple resources (:countries, :states, :schools etc.) but would like a single "Dashboard" controller to handle all the actions.
I would like to be able to do the following:
countries_path would direct me to a show_countries action in the DashboardController and be accesible by '/dashboard/countries.
Likewise for states, schools, etc.
I've read up on Rails routing and have been messing around with various options. I ended up with the following in my routes.rb file:
scope "toolbox" do
resources :countries, :controller => "toolbox", :only => :index do
get 'show_countries', :on => :collection
end
...
end
Running rake routes gives me the following for the code above:
show_countries_countries GET /toolbox/countries/show_countries(.:format) {:action=>"show_countries", :controller=>"toolbox"}
countries GET /toolbox/countries(.:format) {:action=>"index", :controller=>"toolbox"}
I've tried this:
scope "toolbox" do
resources :countries, :controller => "toolbox", :only => :index, :action => "show_countries"
end
only to get this route:
countries GET /toolbox/countries(.:format) {:action=>"index", :controller=>"toolbox"}
What I really want is this:
countries GET /toolbox/countries(.:format) {:action=>"show_countries", :controller=>"toolbox"}
Any ideas?
You just have to think outside of the 'resources' box:
scope "toolbox", :controller => :toolbox do
get 'countries' => :show_countries
get 'states' => :show_states
get 'schools' => :show_shools
end
Should output routes like this:
countries GET /toolbox/countries(.:format) toolbox#show_countries

How can I create custom route helpers for usage in routes.rb

I have a few reoccurring patterns in my routes.rb and I would like to make it DRY by creating a method that creates those routes for me.
An example of what I want to accomplish can be seen in the Devise gem where you can use the following syntax:
#routes.rb
devise_for :users
Which will generate all the routes necessary for Devise. I would like to create something similar. Say for example that I have the following routes:
resources :posts do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'posts#destroy_file', :via => :delete, :as => :destroy_file
end
resources :articles do
member do
get 'new_file'
post 'add_file'
end
match 'files/:id' => 'articles#destroy_file', :via => :delete, :as => :destroy_file
end
This starts getting messy quite quickly so I would like to find a way to do it like this instead:
resources_with_files :posts
resources_with_files :articles
So my question is, how can I create the resources_with_files method?
Put this in something like lib/routes_helper.rb:
class ActionDispatch::Routing::Mapper
def resources_with_files(*resources)
resources.each do |r|
Rails.application.routes.draw do
resources r do
member do
get 'new_file'
post 'add_file'
delete 'files' => :destroy_file
end
end
end
end
end
end
and require it in config/routes.rb

Resources