Routing Error - Custom Contoller Method (Adding Custom routes in resource routes) - ruby-on-rails

I want to add link to friends list but i get routing error
No route matches {:action=>"friend_list", :controller=>"users_controller"}
users_controller
def friend_list
#frnds = User.find_friends(current_user)
end
routes
devise_for :users
resources :users do
member do
get :follow
get :unfollow
get :show
end
match 'users/:id/friend_list' => 'users#friend_list', via: [:get]
link
<li><%= link_to "Friends", :controller => :users_controller, :action => :friend_list%></li>
end
match 'users/:id/friend_list' => 'users#friend_list', via: [:get]
root 'home#front'

instead of member block use collection block like this
resources :users do
collection do
get "follow"
get "unfollow"
get "show"
end
end
member block will append :id in routing while collection block will allow you add custom routes in resource routes
in link_to use this
<%=link_to "Friends", controller: "users", action: "friend_list"%>

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.

custom route in rails 4 not working

I've created custom routes to route to News model
resources :news, only: [:index] do
collection do
get 'page/:page', action: :index
end
end
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
generate url like this
http://localhost:3000/news/4/page/2 index.html.erb is right
and show.html.erb
<%= link_to 'news', news_path(#news, '2')%>
I hope generate url http://localhost:3000/news/4/p/2
but generate http://localhost:3000/news/4?p=2
I think you've got a route conflict, change this:
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
to this:
get "news/:id(/p/:p)", to: 'news#show', as: 'news_page'
and the use this link:
<%= link_to 'News', news_page_path(#news, '2')%>

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Post to Create Action Using link_to

I'm trying to post to my registration controller using a link_to link.
I have <%= link_to "Register for Period", registration_path(period_id: period.id), :method => :post %>
Which generates a link like: http://localhost:3000/registrations/6?period_id=25 where the 6 is the event_id. I need to save the period_id and the user_id to the registration database.
I get the following error in the browser: No route matches [POST] "/registrations/6"
What am I doing wrong?
routes:
Mavens::Application.routes.draw do
devise_for :users
resources :events
resources :periods
resources :products
resources :cart_rows
resources :product_requests
resources :inqueries
resources :registrations
match '/profile', to: 'static_pages#profile'
root :to => 'static_pages#home'
get "static_pages/home"
get "static_pages/about"
end
If you put in your routes.rb:
resources :registrations do
member do
post :save_period
end
end
And in your link:
<%= link_to "Register for period",
save_period_registration_path(id: #registration.id, period_id: period.id), :method => :post %>
You will have a route that matches your resquest.
When you only have a resources :registrations rule on your routes.rb, only the default restful routes are created, and there is no POST to a single resource created by default.
I believe you will have to read something about the CSRF token, because if you have a protect_from_forgery on your application_controller, probably this POST request from a single link would not work.
Your routes.rb is missing the required path. It IS a standard path, so adding 'resources :registrations' would work.
If more complex, post your routes.rb and we can tell what to add.

Reusing the name of a custom RESTful route in Rails 3

I want to do something that I imagine looks like this:
resources :users do
collection do
get 'login', :action => 'login_form'
post 'login', :action => 'login'
get 'logout'
end
end
I.e. I want two controller actions to bind to the same path with different methods. How do I do that?
You should read the guide about routes: http://guides.rubyonrails.org/routing.html
resources :users do
collection do
match 'login' => "users#login_form", via: :get
post 'login'
get 'logout'
end
end
A login_form action does not sound very restful. Just saying ;)

Resources