Rails 3.2. Routing helper generates unvalid links - ruby-on-rails

In routes.rb:
scope "(:locale)", locale: /en|de/ do
get 'service' => 'service#index'
get 'service/:id' => 'service#show'
end
Then in view I use helper service_path(params[:locale], id) and get this link /en/service.1.
But i need link like this /en/service/1.
Check the routing via rake routes:
service GET (/:locale)/service(.:format) service#index (locale=>/en|ru/}
GET (/:locale)/service/:id(.:format) service#show {:locale=>/en|ru/}
How to get normal links like /en/service/1, what I'm doing wrong?

You should rename your controller to pluralize form Services
Change in routes:
get 'service' => 'service#index' to get 'services' => 'services#index', :as => :services
and
get 'service/:id' => 'service#show' to get 'services/:id' => 'services#show', :as => :service
Or you can write simple:
resources :services, :only => [:show, :index]

Related

Devise and Users Route Confilect

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.

Dynamic Routes Settings In Ruby on Rails Like Facebook

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

Route with empty path and helper

I'm trying to implement a "à la github" url like "github.com/josevalim/inherited_resources".
I achieved my goal with this code :
resources :users, :path => '', only: [] do
resources :projects, :path => '', only: [:show]
end
But now, I can't find a way to redirect properly to this route.
When I run a rake routes in the console, all I have is
GET /:user_id/:id(.:format) projects#show
and no name to find a helper method like my old users_project_path.
Have I miss something ? Thanks!
I recently did something similar, try the following
scope '/:user_id', as: :user do
resources :projects, path: '', only: [:show]
end
Running rake routes should then give you
user_project GET /:user_id/:id(.:format) projects#show
and this will give you the proper user_project_path(:user_id, :id) route helper.
You're deleting the path for this reason you can get route_path, use as to establish a route name.
resources :users, :path => '', only: [] do
get :projects, "/:user_id/:id", :to => "project#show", :as => :project_user
end
OR if you don't want use that you can use this code for generating path:
url_for([#user, #project])

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 to define rails 3.1 custom routes

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

Resources