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
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.
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
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
In my routes file, I have a resource called products. The index action of the products resource is also my root path.
resources :products
root :to => "products#index"
When I use the helper method products_path (in a redirect or link), it returns "/products". But what I want is for it to return "/". I know it's the same page, but I want to keep my URL's consistent.
How can I fix this?
Thanks!
root :to => 'products#index', :as => :products
match '', :to => 'products#index', :as => :root # recreate named root_path, if you use it anywhere
This will need to appear below your resources :products as it does in your example above. This will override the products_path and products_url you'd get from resources. Run rake routes before and after to compare.
if you only want to change Index try excluding it first then defining it on its own like so:
resources :products, :except => [:index]
resources :products, :only => [:index], :path => '/'
root_path() should return /
I guess you could redefine the products_path to be the same as root_path (like in a helper file):
def products_path(*params)
root_path *params
end