rails controller with namespace with shorter url - ruby-on-rails

Here is my code
namespace :appname do
resources :docs do
collection do
get 'contact'
get 'how_it_works'
get 'terms'
get 'privacy'
end
end
end
It generates
/appname/docs/contact
/appname/docs/how_it_works
/appname/docs/privacy
/appname/docs/terms
But how to make them as
/docs/contact
/docs/how_it_works
/docs/privacy
/docs/terms
my controller code
class Appname::DocsController < ApplicationController
def how_it_works
end
def privacy
end
def contact
end
def terms
end
end

defined the routes as follow
scope module: 'appname' do
resources :docs do
collection do
get 'contact'
get 'how_it_works'
get 'terms'
get 'privacy'
end
end
end
you can get more info from the rails routing guide in the namespace section. http://guides.ruby-china.org/routing.html

Remove the outer namespace :appname ... end block.

Related

【Rails】How can I write child directory path in route.rb?

The code is below.
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
My wish is that path is hoge/feeds/api_index and can execute api_index action in admin/feeds_controller.
The routes.rb is currently Routing Error.
Because the path is controller/feeds.
How can I call api_index action in controller/admin/feeds?
Thank you.
Error(Add)
I wrote the below
#routes.rb
Rails.application.routes.draw do
scope '/hoge' do
resource :feeds, controller: 'admin/feeds', only: [] do
collection do
get :api_index
end
end
end
end
Then, I got a error
undefined local variable or method `api_index' for Admin::FeedsController:Class
But I definitely wrote def api_index in app/controllers/admin/feeds_controller.rb
How Can I do that?
You can use namespace.
More info in the Rails guide: https://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
#app/controllers/admin/feeds_controller.rb
class Admin::FeedsController < ApplicationController
def api_index
end
end
#routes.rb
Rails.application.routes.draw do
namespace :admin do
scope '/hoge' do
resource :feeds, only: [] do
collection do
get :api_index
end
end
end
end
end

Routing Error uninitialized constant rails

I'm getting this error.When I want to run te server on localhost:3000/api/v1/songs.json
Routing Error
uninitialized constant API::V1::SongsController.
Thats my routes.rb file:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :songs, only: [:index, :create, :update, :destroy]
end
end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
Path Match
api_v1_songs_path GET /api/v1/songs(.:format)
api/v1/songs#index
POST /api/v1/songs(.:format)
api/v1/songs#create
api_v1_song_path PATCH /api/v1/songs/:id(.:format)
api/v1/songs#update
PUT /api/v1/songs/:id(.:format)
api/v1/songs#update
DELETE /api/v1/songs/:id(.:format)
api/v1/songs#destroy
and thats my SongsController:
class Api::V1::SongsController < Api::V1::BaseController
def index
respond_with Song.all
end
def create
respond_with :api, :v1, Song.create(song_params)
end
def destroy
respond_with Song.destroy(params[:id])
end
def update
song = Song.find(params["id"])
song.update_attributes(song_params)
respond_with song, json: song
end
private
def song_params
params.require(:song).permit(:id, :name, :singer_name, :genre, :updated_at, :tag)
end
end
I'm going to copy how we have this running, cause I cannot see a direct difference between your code and our code....
routes.rb
constraints subdomain: Settings.subdomains.api do # you can ignore this
namespace :api do
namespace :v1 do
resources :maps, only: [] do
collection do
get :world_map
end
end
end
end
maps_controller.rb
module Api
module V1
class MapsController < BaseController
def world_map; end
end
end
end
routes output
world_map_api_v1_maps GET /v1/maps/world_map(.:format) api/v1/maps#world_map {:subdomain=>"api"}
The spelling is really crucial from what I can see.
The directort structure for us:
app/controllers/api/v1/maps_controller.rb
So check those points and this should work because it's standard Rails magic.

Routing error for rails - uninitialized constant SubscribersController

I have a Subscriber model that takes in a "phone_number" and a "visit" integer. I have two controllers Subscribers and Visits(super and sub) I have never worked with nested controllers before and I'm having some issues with namespace I believe. Because I getting back the uninitialized constant error. Basically the subscriber controller signs up a subscriber and the visit controller counts the amount of times they've visited by user input of their phone_number. Why am I getting this error? I'll show my code for clarity.
CONTROLLERS
class Subscribers::VisitsController < ApplicationController
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if #subscriber
#subscriber.visit += 1
#subscriber.save
redirect_to subscribers_visits_new_path(:subscriber)
else
render "new"
end
end
end
class SubscribersController < ApplicationController
def index
#subscriber = Subscriber.all
end
def new
#subscriber = Subscriber.new
end
def create
#subscriber = Subscriber.create(subscriber_params)
if #subscriber.save
flash[:success] = "Subscriber Has Been successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end
ROUTES
Rails.application.routes.draw do
devise_for :users
resources :subscribers, except: :show
get '/subscribers/visits/new', to: 'subscribers/visits#new'
root "welcomes#index"
VIEWS
<h1>hey</hey>
<%= form_for #subscriber do |form| %>
<div class="form-group">
<p>
<%= form.label :phone_number %>
<%= form.text_field :phone_number %>
</p>
<% end %>
ERROR
Hmm, my guess is you are trying to route url subscriber/visits/new to new action in VisitsController?How about changing this line:
get '/subscribers/visits/new', to: 'subscribers/visits#new'
to:
namespace :subscribers do
get '/visits/new', to: 'visits#new'
end
Also try to move this block above resources :subscribers, except: :show if you still get the error.
Cheers
You probably do not need to inherit one controller from another. Simply define the controllers as you normally would:
app/controllers/subscribers_controller.rb
class SubscribersController < ApplicationController
# methods for Subscribers
end
in app/controllers/visits_controller.rb
class VisitsController < ApplicationController
# methods for Visits
end
Note that these must to be located in separate files, so that Rails can find the correct source file by the name of the object that it's looking for. This is a Rails naming convention.
Regarding your routes, you'll need to change to use one of 4 route formats. Reading the section on Adding More RESTful Actions in the Rails Routing from the Outside In guide might help.
1) To route visits as a nested resource, which is what it appears you're actually trying to do, you would use this:
resources :subscribers, except: :show do
resources :visits
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
GET /subscribers/:id/visits
GET /subscribers/:id/visits/:id
GET /subscribers/:id/visits/:id/edit
PATCH /subscribers/:id/visits/:id/update
DELETE /subscribers/:id/visits/:id/destroy
This is the typical route structure for nested resources and separate controllers.
2) To make visits#new a simple collection (non-member) action in the VisitsController, then you likely want this:
resources :subscribers, except: :show do
collection do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/visits/new
POST /subscribers/visits
This is typically used to add new top-level routes in an existing resource and controller.
3) To construct visits as member actions, use this:
resources :subscribers, except: :show do
member do
get 'visits/new', to 'visits#new'
post 'visits', to 'visits#create'
end
end
This will produce these routes:
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
GET /subscribers/:id/visits/new
POST /subscribers/:id/visits
This is normally used to add new member routes in an existing resource and controller.
4) To simply make visits routes appear to be included in subscribers, you could use this:
get '/subscribers/visits/new', to: 'visits#new'
post '/subscribers/visits', to: 'visits#create'
resources :subscribers, except: :show
This will produce these routes:
GET /subscribers/visits/new
POST /subscribers/visits
GET /subscribers/new
POST /subscribers
GET /subscribers
GET /subscribers/:id/edit
PATCH /subscribers/:id/update
DELETE /subscribers/:id/destroy
This may be used to make arbitrary routes appear to be included in an existing resource, when they really may be independent.

Acts as follower Set Up

I'm trying to get acts_as_follower working but i'm missing something. A user should be able to Follow another User.
I added the Gem :
gem "acts_as_follower", '~> 0.2.0' #0.2.0 for Rails 4
In my User model i added :
acts_as_follower
acts_as_followable
The User Controller i created looks like this :
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def follow
#user = User.find(params[:id])
current_user.follow(#user)
redirect_to :back
end
end
My routes :
App::Application.routes.draw do
root 'screens#index'
devise_for :users
get 'u/:id' => 'users#show', as: :user
resources :screens, :path => 's' do
member do
get :like
get :unlike
end
end
get "pages/home"
get "about" => "pages#about"
get "users/show"
end
now in the routes i have to add the member :follow =>
member do
get :follow
end
But i'm stuck at this point. I tried a few variations and my link to follow a User is :
<%= link_to "Follow", follow_user_path(#user)%>
and this is giving me the error ->
undefined method `follow_users_path' for #<#<Class:0x007fb4da78e920>:0x007fb4db3facb8>
The Solution was to create a user resource, it works perfectly now =>
resources :users do
member do
get :follow
get :unfollow
end
end

Resourceful Routes helpers _path and _url dont work

I am trying to redirect user to show_city_url or show_city_path but i get an exception that they are both undefined.In the city controller i have three actions show,like, and dislike. unlike_city_path and like_city_path works but show_city_path doesnt.Also when i put this in all_cities action redirect_to :controller=>"city",:action=>"show" works.What am i doing wrong?Thank you.
class HomeController < ApplicationController
def all-cities
redirect_to show_city_url
end
end
In the Routes
resources :city do
member do
post :like
post :dislike
get :show
end
end
according to your comments:
resources :cities, :controller => 'city' do
collection do
get :show, :as => :show
end
member do
post :like
post :dislike
end
end
now you can call show_cities_url and you'll land in the show action of your CityController.
PS: Following the Rails' convention makes your life easier ;)
RoR Guide: Rails Routing from the Outside In

Resources