When I try to rake a migration for my ruby project, it gives me an error "'API::sessions' is not a supported controller name". Does anyone know how to solve this problem? The following code is from my routes.rb file and sessions_controller.rb file.
Rails.application.routes.draw do
get 'projects/:id', to: 'projects#show'
get 'projects', to: 'projects#index'
get 'welcome/index'
root 'welcome#index'
match '/login', to: 'sessions#new', via: :get
match '/login_create', to: 'sessions#create', via: :post
resources :users
scope :format => true, :constraints => { :format => 'json' } do
post "/api/login" => "API::sessions#create"
delete "/api/logout" => "API::sessions#destroy"
end
end
class API::SessionsController < API::ApiController
skip_before_action :require_login, only: [:create], raise: false
def create
if user = User.valid_login?(params[:email], params[:password])
allow_token_to_be_used_only_once_for(user)
send_auth_token_for_valid_login_of(user)
else
render_unauthorized("Error with your login or password")
end
end
def destroy
logout
head :ok
end
private
def send_auth_token_for_valid_login_of(user)
render json: { token: user.token }
end
def allow_token_to_be_used_only_once_for(user)
user.regenerate_token
end
def logout
current_user.invalidate_token
end
end
The error appears when you define the post and delete routes within the format scope, to tell Rails which controller and action/method use for the URI being defined.
To refer then the controller name and action use the lowercase name followed by a slash, instead the uppercase controller name and colons ::, like:
post '/api/login' => 'api/sessions#create'
delete '/api/logout' => 'api/sessions#destroy'
Related
I'm having some issues with custom routing. What I'm looking to do is remove the model from the route and dynamically use the record name.
so instead of:
site.com/events/my-event
I would like it to be:
site.com/my-event
I hacked this to work with the below code, only issue is I can't access my admin namespace as it's being treated as an event record (and any other route):
get('/:id', to: redirect do |params, request|
id = request.path.gsub("/", "")
"/events/#{id}"
end)
I know this redirect is not right, I'm just not well versed in routing options. How should this be done properly?
routes.rb
Rails.application.routes.draw do
resources :events, param: :id, path: "" do
get 'login', to: 'sessions#new', as: :login
get 'logout', to: 'sessions#destroy', as: :logout
post 'sessions', to: 'sessions#create', as: :session_create
end
namespace 'admin' do
root "events#index"
resources :sessions, only: [:create]
get 'login', to: 'sessions#new', as: :login
get 'logout', to: 'sessions#destroy', as: :logout
resources :events
end
end
Rails lets you specify what a routing URL should look like:
https://guides.rubyonrails.org/routing.html#translated-paths
You don't need redirect.
You need to match all requests to one controller/action.
get '/(*all)', to: 'home#app'
Inside this action check what is coming in params and render appropriate view.
PS: it will capture all requests, even for not found images, js, etc.
(1) To fix /admin to go to the right path, move the namespace line above the events resources so that it is matched first:
Rails routes are matched in the order they are specified 2.2 CRUD, Verbs, and Actions
# routes.rb
namespace :admin do
root "events#index"
#...
resources :events
end
resources :events, param: :name, path: ""
Use :param and :path to match for site.com/myevent instead of site.com/events/:id
Your events controller:
# events_controller.rb
class EventsController < ApplicationController
# ...
private
def set_event
#event = Event.find_by(name: params[:name])
end
end
Your admin events controller:
# admin/events_controller.rb
class Admin::EventsController < ApplicationController
# ...
private
def set_event
#event = Event.find params[:id]
end
end
TIP: To get a complete list of the available routes use rails routes in your terminal 5.1 Listing Existing Routes
My rails application successfully logs out a user and redirects to the about page when in development mode. But the moment i deploy it to production it returns a 302 status and a page that shows "you are being redirected". Am using devise for authentication below is what my code actually looks like.
The routes
Rails.application.routes.draw do
devise_for :users, controllers: { registrations: "users/registrations", confirmations: "users/confirmations", sessions: "devise/sessions" }, skip: [:sessions]
devise_scope :user do
get "login" => "devise/sessions#new", as: :new_user_session
post "login" => "devise/sessions#create", as: :user_session
get "/join" => "users/registrations#new", as: :join
get '/logout', to: "devise/sessions#destroy", as: :destroy_user_session
end
resources :companies
get "/about", to: "pages#about"
get "/faq", to: "pages#faq"
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'pages#home'
end
My application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
protected
def after_sign_in_path_for(resource)
company_path(resource)
end
def after_sign_out_path_for(resource_or_scope)
"/about"
end
def store_location
session[:return_to] == request.full_path
end
def clear_stored_location
session[:return_to] = nil
end
def redirect_back_or_to(alternate)
redirect_to(session[:return_to] || alternate)
clear_stored_location
end
end
And finally my link to log out.
<%= link_to "Log out", destroy_user_session_path %>
Please note that i am using devise 4.2.0 and capistrano for deployment.
Regards
~
I had to override devise sessions#controller and changed my routes to:
delete 'logout', to: "users/sessions#destroy", as: :destroy_user_session
Then i added delete method to my link for logging out.
Using locale in my rails app. I have the following routes.rb
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root to: 'static_pages#home'
devise_for :users, :controllers => { :registrations => :registrations }
resources :blogs do
resources :comments
end
get 'tags/:tag', to: 'blogs#index', as: :tag
resources :users
get '/users/subregion_options' => 'users#subregion_options'
resources "contacts", only: [:new, :create]
match '/crew', to: 'static_pages#crew', via: 'get'
....
end
match '*path', to: redirect("/#{I18n.locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }, via: 'get'
I would like the user accessing the page to get redirected to the locale depending on language browser settings (http header). This all works - basically. However, accessing localhost:3000/ application_controller.rb is not being called -therefore the set_locale is not being executed and that means the user ends up with the welcome rails page
How can I force the set_locale to be called?
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
if params[:locale].blank?
redirect_to "/#{extract_locale_from_accept_language_header}"
else
I18n.locale = params[:locale]
end
end
private
def extract_locale_from_accept_language_header
case request.env['HTTP_ACCEPT_LANGUAGE'].try(:scan, /^[a-z]{2}/).try(:first).try(:to_sym)
when 'en'
'en'
when 'de'
'de'
when 'de-at'
'de'
when 'de-ch'
'de'
else
'en'
end
end
def default_url_options(options = {})
{locale: I18n.locale}
end
I have somehow resolved the issue (not entirely sure if that is the most elegant way), by creating a redirect to trigger set locale:
In my routes.rb I added the following outside the locale scope:
root to: 'static_pages#redirect'
Here the full version
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
match '', to: 'static_pages#home', :as => 'home', via: 'get''
devise_for :users, :controllers => { :registrations => :registrations }
resources :blogs do
resources :comments
end
get 'tags/:tag', to: 'blogs#index', as: :tag
resources :users
get '/users/subregion_options' => 'users#subregion_options'
resources "contacts", only: [:new, :create]
match '/crew', to: 'static_pages#crew', via: 'get'
....
end
I do have only one root now. I also changed the root_to to match within the locale scope, using :as => home so I can refer to it in link_to from the views as home_path.
So when user enters the xxxx.com - he/she will end up in redirect which does the following:
class StaticPagesController < ApplicationController
def redirect
redirect_to home
end
def home
end
Since the locale is blank it will now check for locale in http header and if users preferred language does exist he/she will get directed to the right page/language. For subsequent requests (i.e.: when user clicks a link) the locale will be read out from the URL parameter.
If there's a more elegant solution to this feel free to answer - points are still up for grabs.
So I keep getting the error:
No route matches {:action=>"create", :controller=>"xaaron/api_keys"}
Which is thrown in the test:
it "should not create an api key for those not logged in" do
post :create
expect(response).to redirect_to xaaron.login_path
end
when I go to spec/dummy and run the rake routes command I see:
api_keys GET /api_keys(.:format) xaaron/api_keys#index
POST /api_keys(.:format) xaaron/api_keys#create
new_api_key GET /api_keys/new(.:format) xaaron/api_keys#new
edit_api_key GET /api_keys/:id/edit(.:format) xaaron/api_keys#edit
api_key GET /api_keys/:id(.:format) xaaron/api_keys#show
PATCH /api_keys/:id(.:format) xaaron/api_keys#update
PUT /api_keys/:id(.:format) xaaron/api_keys#update
DELETE /api_keys/:id(.:format) xaaron/api_keys#destroy
Which shows that yes this route does exist. My routes file for this engine looks like:
Xaaron::Engine.routes.draw do
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'signup' => 'users#new', :as => 'signup'
get 'permission_denied' => 'error#denied', :as => 'permission_denied'
get 'record_not_found' => 'error#error', :as => 'record_not_found'
get 'password_reset' => 'password_resets#edit', :as => 'rest_user_password'
resource :error, controller: 'error'
resources :users
resources :api_keys
resources :sessions
resources :roles
resources :password_resets
end
What am I missing?
update
For those of you curious how I am getting these routes, its because the dummy app's routes file is set up (by default) as such:
Rails.application.routes.draw do
mount Xaaron::Engine => "/xaaron"
end
Update II
I have been reading this api docs on how routing is done in engines and I believe the way I have done this is correct, how ever the controller is defined as such:
module Xaaron
class ApiKeysController < ActionController::Base
before_action :authenticate_user!
def index
#api_key = Xaaron::ApiKey.where(:user_id => current_user.id)
end
def create
#api_key = Xaaron::ApiKey.new(:user_id => current_user.id, :api_key => SecureRandom.hex(16))
create_api_key(#api_key)
end
def destroy
Xaaron::ApiKey.find(params[:id]).destroy
flash[:notice] = 'Api Key has been deleted.'
redirect_to xarron.api_keys_path
end
end
end
You need to tell your spec you are using the engine routes:
describe ApiKeysController do
routes { Xaaron::Engine.routes }
it "should not create an api key for those not logged in" do
post :create
expect(response).to redirect_to xaaron.login_path
end
end
I have a refinery extension that I created that manages Fieldtrips for a school.
My current fieldtrips_controller for the admin side has the following.
module Refinery
module Fieldtrips
module Admin
class FieldtripsController < ::Refinery::AdminController
crudify :'refinery/fieldtrips/fieldtrip', :xhr_paging => true
def destory_pin
#pin = Refinery::Pins::Pin.find(params[:id])
if #pin.delete
render :json => {result: "success"}
else
render :json => {result: "error"}
end
end
end
end
end
end
As you can see I added a method for destroying what I'm calling a pin, witch is a model that I have associated with fieldtrips.
I'm using an ajax request from my Fieldtrip edit page. the url I'd like to call would be "refinery/fieldtrips/destory_pin
currently in my routes file for fieldtrips I have the following
Refinery::Core::Engine.routes.append do
# Frontend routes
namespace :fieldtrips do
resources :fieldtrips, :path => '', :only => [:index, :show]
end
# Admin routes
namespace :fieldtrips, :path => '' do
namespace :admin, :path => 'refinery' do
resources :fieldtrips, :except => :show do
collection do
post :update_positions
post :destory_pin
end
end
end
end
end
Currently when I run rake routes I get the following
POST /refinery/fieldtrips/destory_pin(.:format) refinery/fieldtrips/admin/fieldtrips#destory_pin
If I nav to /refinery/fieldtrips/destory_pin in my browser I get a NoMethodError in Refinery::Admin::BaseController#error_404
I'm sure I'm doing the routes wrong and need suggestions on how to correct this.
Use member instead of collection, e.g.
namespace :fieldtrips, :path => '' do
namespace :admin, :path => 'refinery' do
resources :fieldtrips, :except => :show do
collection do
post :update_positions
end
member do
post :destory_pin
end
end
end
end