Menu

Ror Routes + Custom Routes

April 6, 2015 - Rails

ADD ROUTES IN APP BY :-

1) Go to -> YourApp -> config -> routes.rb (Paste Code Here) i.e.

root ‘blogs#home’
match ‘/about’, to: ‘pages#about’, via: ‘get’
match ‘/contact’, to: ‘pages#contact’, via: ‘get’ 

resources :subscriptions do
collection do
get :get_user
end
end

* Some Imp. Things we know about route and how this code work :-

A) Main Route :-

1) root ‘blogs#home’  -> root(Home Page route set by this), ‘blogs#home’  -> (blogs is folder in view and home is page or (action))

To run this route we create action in controller by :-

Go to -> Yourapp -> app -> controller -> Your_controller (e.g-blogs_controller.rb) (Paste code here) i.e

def home

end

B) Custom Route :-

1) match ‘/about’, to: ‘pages#about’, via: ‘get’  -> match ‘/about'(this code match value of custom route),

to: ‘pages#about’ (to: where page come from), ‘pages#about'(pages is folder in view and about is page to show),

via: ‘get’ (we send request by two one is get and second is post but we use “get”).

2) match ‘/contact’, to: ‘pages#contact’, via: ‘get’ -> match ‘/contact'(this code match value of custom route),

to: ‘pages#contact’ (to: where page come from), ‘pages#contact'(pages is folder in view and contact is page to show),

via: ‘get’ (we send request by two one is get and second is post but we use “get”).

3) resources :subscriptions do
collection do
get :get_user
end
end

(resources to get file we use it ), (:subscriptions do subscription is a page to get in app and do use to call), (collection do get full collection), (get :get_user get use to call request and get_user for the subscription ) (end to end the route).

To run this route we create action in controller by :-

Go to -> YourApp -> app -> controller -> Your_controller (e.g- subscriptions_controller.rb) (Paste code here) i.e

def get_user
@subscription = Subscription.new
end

Leave a Reply