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
Related
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'
I followed this rails cast to create authentication for a rails project. My routes currently look like this:
Rails.application.routes.draw do
resources :messages
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
get "new_photo" => "users#edit", :as => "new_photo"
root :to => "users#new"
resources :users
resources :sessions
end
How to I edit this file so that the root will be pointing to "messages#new", if a user is logged in and "users#new" when no user is logged in? I tried many of the solutions on other pages, but they didnt work (they were probably for devise). Thanks for the help!
You'll probably want to handle this in your controller.
routes.rb
root :to => "users#new"
users_controller.rb
def new
return redirect_to new_messages_url if current_user
# normal controller code below...
end
This will redirect the logged_in user (current_user) to the new messages page if already logged in. I'm just assuming that current_user holds your user data, it may be different for your application.
write it in any home controler.
def set_roots
if current_user
redirect_to dashboard_home_index_path
else
redirect_to home_index_path
end
end
in routes.rb file
root :to => 'home#set_roots'
match "/find_roots" => "home#set_roots"
I have had a Rails 3.2.12 application, now I'm gonna go updation step by step to 4.2 ... now I'm done the first step to 4.0.0 ...
Now I've got some routing problems. My pages are realized like a REST api for example
wwww.test.de/users/7
that works fine all pages where shown perfektly ... but when I want to save new data via Post it dosn't work got nil errors. ...
I've tried this to fix the Problem in the routes.rb
get '/:controller(/:action(/:id))'
post '/:controller(/:action(/:id))'
But that dosn't work ... Is there a way how to fix this problem?
Update:
Routes:
XYZ::Application.routes.draw do
resources :api_users, :as => :users
resources :api_courses, :as => :courses
resources :api_uploads, :as => :uploads
get "/super_admin(/:action(/:id))", :to => "super_admin", :constraints => {:subdomain => "admin"}
get "/", :to => redirect("/super_admin"), :constraints => {:subdomain => "admin"}
get "/super_admin(/:action(/:id))", :to => "super_admin", :constraints => {:subdomain => "admin.staging"}
get "/", :to => redirect("/super_admin"), :constraints => {:subdomain => "admin.staging"}
get "/super_admin(/:action(/:id))", :to => redirect("/")
get '/' => 'pages#home'
get '/:controller(/:action(/:id))'
post '/:controller(/:action(/:id))'
end
Controller funktion which is called:
def new
#course = CourseObject.find params[:id]
return false unless check_authorization #course
add_breadcrumb_for_course_object #course
add_breadcrumb "breadcrumb.new_lesson", :action => :new
#lesson = Lesson.new params[:lesson]
puts request.request_parameters
if request.post?
#lesson.sort = Lesson.count
if #lesson.save
#course.lessons << #lesson
flash[:notice] = I18n.t "flash.saved"
redirect_to :action => :edit, :id => #lesson.id
end
end
end
Parameters are printed like I typed in in my form ...
.... "lesson"=>{"name"=>"sfsdf"}}
I am working on a Team Treehouse Ruby on Rails app that emulates a basic Facebook app.
I am now added social feature's like friends.
I recently added the following link to one of my views:
<%= link_to "Add Friend", new_user_friendship_path(friend_id: #user), class: 'btn' %>
When click the resulting button, I get the following error:
Routing Error
uninitialized constant UserFriendshipsController
Try running rake routes for more information on available routes.
I am thinking the problem is in either my "user_friendship_controller.rb" of "config/routes.rb " file.
Here is my "user_friendship_controller.rb file:
class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!, only: [:new]
def new
if params[:friend_id]
#friend = User.where(profile_name: params[:friend_id]).first
#user_friendship = current_user.user_friendships.new(friend: #friend)
else
flash[:error] = "Friend required"
end
rescue ActiveRecord::RecordNotFound
render file: 'public/404', status: :not_found
end
end
And here is my "config/routes" file:
Treebook::Application.routes.draw do
as :user do
get '/register', to: 'devise/registrations#new',via: :get, as: :register
get '/login', to: 'devise/sessions#new', via: :get, as: :login
get '/logout', to: 'devise/sessions#destroy', via: :delete, as: :logout
end
devise_for :users, :skip => [:sessions]
as :user do
get '/login' => 'devise/sessions#new', as: :new_user_session
post '/login' => 'devise/sessions#create', as: :user_session
delete '/logout' => 'devise/sessions#destroy', as: :destroy_user_session
end
resources :user_friendships do
end
resources :statuses
get 'feed', to: 'statuses#index', as: :feed
root to: 'statuses#index'
get '/:id', to: 'profiles#show', as: 'profile'
end
Any help figuring out this bug is a great help.
Thanks
It looks like it's just a simple mis-naming of your file.
Try renaming it to user_friendships_controller.rb. Rails expects your class declaration to match the file name (as it stands it'd be looking for you to define UserFriendshipController).
I have a small API that I am writing in rails 3. This is not a restful application so I use a controller called api that has some methods such as:
def users
#users = User.all
respond_to do |format|
format.any do
render :xml => #users.to_xml
end
end
end
end
My routes (with comments etc removed) file looks like this:
resources :shows
resources :users
resources :comments
devise_scope :user do
get "/login" => "devise/sessions#new"
get "/logout" => "devise/sessions#destroy"
get "/register" => "devise/registrations#new"
end
root :to => 'home#index'
match ':controller(/:action(/:id(.:format)))'
When I call api/users I get the XML wrapped in HTML tags (in the body actually) but if I call api/users.xml I get a 406 error?
Do I need to change my routes to accommodate the XML call?
Thanks,
s
I added a new route to routes.rb and it worked:
match 'api/users.:format' => 'api#users', :constraints => {:format => /(xml)/}
j