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
Related
I get the following clicking that link
<%= link_to "(#{User.count_friend_requests(current_user)}) Friends Requests", :controller => "users", :action=>"friend_requests"%>
i get that error
ActionController::UrlGenerationError in Devise::Registrations#edit
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
routes
devise_for :users, :path_prefix => 'my'
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
users_controller
def friend_requests
#frnds = User.find_friend_requests(current_user)
end
Heh, you've mounted devise with prefix /my/, so your routes are like
/my/users/..
and resources :users mounts your code without any prefix.
You should remove prefix my or or change your resources to
scope "my" do
resources :users, :only => [:friend_requests] do
get "friend_requests", :on => :collection
end
end
to get path like /my/users/friend_requests/
Also I see that rails looks for your method in devise/users (not in your users) controller
No route matches {:action=>"friend_requests", :controller=>"devise/users", :format=>"1"}
If you want to patch it, you should take a look how to patch a devise's users controller.
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
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
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
I want to route http://localhost:3000/users/1/rename/alex to my users controller with rename action.
what I did was:
match 'users/:id/rename/:name' => 'users#rename', but this is not working, the part after 'users/:id/' is not mapped at all, since I cannot get name by params[:name]
Update:
In routes.rb
resources :users do
put 'rename/:code', :action => :rename, :code => /\w{5}/, :on => :member
end
and,
$ rake routes
...
PUT /users/:id/rename/:code(.:format) {:code=>/\w{5}/, :action=>"rename", :controller=>"users"}
...
If you have resources :users, put your match line before it.
Alternatively, you can pass a block to resources:
resources :users do
match 'rename/:name' => 'users#rename', :on => :member
end