I have following rails3 routes that I want to convert into rails4.
map.with_options(:conditions => {:subdomain => AppConfig['admin_subdomain']}) do |subdom|
subdom.root :controller => 'subscription_admin/subscriptions', :action => 'index'
subdom.with_options(:namespace => 'subscription_admin/', :name_prefix => 'admin_', :path_prefix => nil) do |admin|
...
end
end
namespace :admin do
constraints subdomain: AppConfig['admin_subdomain'] do
root to: 'subscription_admin/subscriptions#index'
namespace :subscription_admin do
resources :some_resources
# RESTful routes
end
end
end
end
More info about constrains.
Related
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"}}
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
I'm a newbie in the Rails, but I can't figure this out.
Rails maps url_for(:controller => 'login', :action => 'check')
to
"/assets?action=check&controller=login"
It should be mapped to /en/login/check (coming from /en/login/index)
My routes.rb:
MyApplication::Application.routes.draw do
scope "(:locale)", :locale => /en|de/ do
resources :login do
get 'index', :on => :member
get 'check', :on => :member
end
end
match ':locale/:controller/:action/:id'
match ':controller/:action/:id'
match ':locale/:controller/:action/:id.:format'
match ':controller/:action/:id.:format'
root :to => 'main#index'
end
I'll read some more about routing, but I'm really confused where /assets comes from and why it's not mapped correctly.
You need to specify the :locale in the url_for helper, else the router won't actually find a match.
url_for(locale: 'en', controller: 'login', action: 'index')
Given the following routes.rb file:
# Add Admin section routes
map.namespace :admin do |admin|
admin.resources :admin_users
admin.resources :admin_user_sessions, :as => :sessions
admin.resources :dashboard
# Authentication Elements
admin.login '/login', :controller => 'admin_user_sessions', :action => 'new'
admin.logout '/logout', :controller => 'admin_user_sessions', :action => 'destroy'
# Default is login page for admin_users
admin.root :controller => 'admin_user_sessions', :action => 'new'
end
Is it possible to alias the 'admin' section to something else without having to change every redirection and link_to in the application? The main reason is that it's something I'd like to be configurable on the fly and hopefully make it also a bit less easy to guess.
map.namespace method just sets some common options for routes inside its block. It uses with_options method:
# File actionpack/lib/action_controller/routing/route_set.rb, line 47
def namespace(name, options = {}, &block)
if options[:namespace]
with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
else
with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
end
end
So it is possible to use with_options method directly instead of namespace:
map.with_options(:path_prefix => "yournewprefix", :name_prefix => "admin_", :namespace => "admin/" ) do |admin|
admin.resources :admin_users
# ....
end
And you can continue to use routes the same way as before, but prefix will be "yournewprefix" instead of "admin"
admin_admin_users_path #=> /yournewprefix/admin_users
In order to create an alias to the namespace (calling one api_version for example, from another router address) you can do the following:
#routes.rb
%w(v1 v2).each do |api_version|
namespace api_version, api_version: api_version, module: :v1 do
resources :some_resource
#...
end
end
this will cause the routes /v1/some_resource and /v2/some_resource to get to the same controller. then you can use params[:api_version] to get the evrsion you need and respond accordingly.
Like in any other resource, :path seem to be working fine for me.
namespace :admin, :path => "myspace" do
resources : notice
resources :article do
resources :links , :path => "url"
end
end
end