Redirecting rake routes - ruby-on-rails

I have this routes and it's routeing to /USERS/:username once all is said and done and I need it to route to /:username without the leading USERS.
resources :users do
get :autocomplete_user_name, :on => :collection
end
autocomplete_user_name_users GET /users/autocomplete_user_name(.:format) users#autocomplete_user_name

You can do:
resources :users, path: '' do
get :autocomplete_user_name, :on => :collection
end

Related

locale parameter changes his location in link

I have some serius problem, I created multiple language selection that works great, but the link is changing all the time.
From http://localhost:3000/en/products when I click such link
<%= link_to image_tag('en.png',:alt => 'description', :title => (I18n.t 'english') ), params.merge(:locale => :en) %>
To something like this after some time I am navigating inside app and clicking different links
http://localhost:3000/manufacturer_products?locale=en&manufacturer=Christophel
I believe that the problem is inside the routes.rb file.
This is my routes file.
root :to => 'home#index'
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
get "about_us/index"
namespace :products do
resources :categories do
resources :products
end
resources :products, only: :index
end
namespace :products do
resources :manufacturs do
resources :products
end
resources :products, only: :index
end
get "about/index"
match ":locale/products/search" => "products#search"
match "/contacts", to: "contacts#index"
match "/products", to: "products#index"
match "/home", to: "home#index"
match "/about_us", to: "about_us#index"
match "/news", to: "news#index"
match "/manufacturer_products", to: "manufacturer_products#index"
match '/:locale' => 'home#index'
scope "(:locale)", :locale => /en|lv|ru/ do
resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products
end
I understand that I have to somehow merge namespace:products route with locale route, but I have no idea to start with, If somebody could give me a tip or smth :)
Thanks
I think your routes are not a problem.
params.merge(:locale => :lv) has the function of joining 2 hashes, so you should just use something like
params[:locale] || :en
I found solution, the thing was that in routes file I didn't have right order of the routes.
This route that handles routing with locales I had at the end off file. So it didn't work.
scope "(:locale)", :locale => /en|lv|ru/ do
resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products, :about_us, :contacts
end
I moved this code to begining of the file and now it works perfectly. :)

what does this rails code do? :on => :member

please can you explainme what does this code do?
resources :products do
get :who_bought, :on => :member
end
the code complete is from the book pragmatig programing, but it not explain why we use that code, ":on => :member""
Depot::Application.routes.draw do
resources :orders
resources :line_items
post 'line_items/decrease'
resources :carts
get "store/index"
resources :products do
get :who_bought, :on => :member
end
root :to => 'store#index', :as => 'store'
thanks
passing :on => :member means that you are working on a specific record in the database, in this case products. so the url that route generates is
/products/:id/who_bought
which means that you want the get the product whose id is :id and process the who_bought action. the counterpart, :on => :collection, expects the action to work on a list of products so the url will look like
/products/who_bought
if you change member to collection. you can see that the route doesn't require an :id passed because it doesn't expect you to work on a single record.

Simple Rails routing, doing a get 'action' inside resource block

Consider this in routes.rb
resource :places do
resource :people
get 'search'
end
When I do this in browser:
localhost:3000/places/search
It gives me invalid id for place error.
I'm looking to do this without using a "match"
You need to specify that the get applies to the collection:
resource :places do
resource :people
get 'search', :on => :collection
end
See the documentation on collection routes for details.
resources :places do
resources :people
get 'search', :on => :collection
end

Rails routes error. No route matches [POST] "/sessions/new"

Hello im doing tutorial from Rails Book and get into some troubles.
While im trying to log in with my username and password im getting the following error:
Routing Error
No route matches [POST] "/sessions/new"
Try running rake routes for more information on available routes.
This is my route file config.
ZomfgShop::Application.routes.draw do
get "admin/index"
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :products
root to: 'store#index', as: 'store'
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
You have controller instead of resources for the :sessions routes.
resources :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
Try replacing
get "sessions/new"
get "sessions/create"
get "sessions/destroy"
with
resources :sessions
make sure you have a views/sessions/new.html.erb file (under views/devise/ if using devise)
and make sure you have this in sessions_controller.rb if you aren't using Devise
def new
end
def create
end
def destroy
end
Thank you guys, changed my routes.rb file to this and my routing fine is working fine once again!
ZomfgShop::Application.routes.draw do
get 'admin' => 'admin#index'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
scope '(:locale)' do
resources :users
resources :orders
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
root to: 'store#index', as: 'store'
end
resources :line_items do
#member do
# post 'decrement'
#end
post 'decrement', on: :member
end

Namespaced controller redirect urls

i have probably a simple question. I have created a namespace panel with categories controller.
After creating or editing a category, rails redirects me to website.com/categories/:id instead of website.com/panel/categories/:id.
I've noticed that in the _form view, the #panel_categories argument of form_for() function points to /categories nor /panel/categories and that's causing this behaviour. Offcourse i can add a :url => '/panel/categories' param but i feel that it's not the best solution...
Can you provide me any better solution?
Thanks in advance
Files:
routes.rb:
Photowall::Application.routes.draw do
resources :photos
resources :categories
resources :fields
resources :users, :user_sessions
match 'login' => 'user_sessions#new', :as => :login
match 'logout' => 'user_sessions#destroy', :as => :logout
namespace :panel do
root :to => "photos#index"
resources :users, :photos, :categories, :fields
end
namespace :admin do
root :to => "users#index"
resources :users, :photos, :categories, :fields
end
end
categories_controller.rb:
http://pastebin.com/rWJykCCF
model is the default one
form:
http://pastebin.com/HGmkZZHM
form_for [:panel, #panel_category]
You can set the url to a route such as:
:url => panel_categories_path
I'm not sure what your route is, but this should work with your application.

Resources