I want to define a new routes when a module be included, I try the following code but it didn't work:
included do
ActionDispatch::Routing::RouteSet.draw do
get "test" => "public#test"
end
end
Please correct me, thanks!
Related
As the title suggests I'm splitting up my routes file, I have 7 split files, the first 6 work and the routes have been extended to those files, however, for some reason one of the files doesn't work.
This is what I have
routes.rb
Rails.application.routes.draw do
..
extend Crm # works
extend Customers # works
extend Suppliers # works
extend Employees # works
extend Bank # doesn't work
extend Accounts # works
extend Admin # works
..
end
I have the following in the config/routes/bank.rb file
module Bank
def self.extended(router)
router.instance_exec do
# bank routes
resources :bank_accounts, except: %i[destory] do
member do
get :import_transactions
get :reconcile
post :process_reconcile
end
collection do
get :list
post :import_transactions_confirm
post :process_import_transactions_confirm
get :transfer
post :process_transfer
get :revalue_currency_bank_account
post :process_revalue_currency_bank_account
end
end
end
end
end
This is the same layout for all the other files so I'm unsure why this file is causing an error.
The error I'm getting is
wrong argument type Class (expected Module)
Bank can only be defined once, either as a class or a module. You probably already have a Bank class somewhere else in your app. If you temporarily remove the extend Bank line, go into the Rails console, and check this:
Bank.class
If that returns Class then it means your app has already defined Bank as a class. You'll need to choose a different module name for your routes, like Banks:
module Banks
def self.extended(router)
...
I'm running into the infamous No Method Error. I've worked my way through a number of examples here on STOF but I can't see an error in my code that stands out. I've checked that rake routes matches what I think should be happening and the paths provided from using resources in the routes.db file seem to be correct. I know I'm missing some small detail but I can't for the life of me see it now. Any help would be appreciated.
My Controller code:
class GenevarecordsController < ApplicationController
def index
#genevarecords = GenevaRecord.all.page(params[:page]).per(5)
end
def new
#genevarecord = GenevaRecord.new
end
end
My routes:
Rails.application.routes.draw do
root 'genevarecords#index'
resources :genevarecords
end
You have a naming discrepency between your model and your controller / routes.
your model is GenevaRecord, underscored makes it geneva_record. However your controller only has a single capital letter at the beginning: Geneverecords which underscored would be genevarecords. Therefore when you pass your model to the form it tries to use a controller / routes helpers with the same naming format as the model, which would be geneva_records_controller ie. GenevaRecordsController.
What you need to do is match your controller and routes to the same naming format as your model:
class GenevaRecordsController < ApplicationController
#...
end
Rails.application.routes.draw do
#...
resources :geneva_records
end
You need to take Did you mean? section seriously,
Anyway, if you closely look at ruby syntax following is the representation for the class name,
AbcDef and equivalent snake case is abc_def
In your case,
Your model is named as GenevaRecord but your controller is GenevarecordsController
change it to GenevaRecordsController, also you need to match it's equivalent snake case in routes...
Rails.application.routes.draw do
root 'geneva_records#index'
resources :geneva_records
end
So, when you pass #genevarecord to the form it is initialized as GenevaRecord.new and searches for geneva_records_path which is undefined because you have defined it as genevarecords_path which doesn't match you model (resources)..
Hope it helps in understanding..
I am building a gem for Rails and would like to make a method available to the routes of the Rails app that uses my gem.
Basically, I want whoever uses my gem to be able to say
websockets_for :messages
which will create a bunch of routes for them. I am unsure where to define that method so that it's available in the routes file.
The method looks something like this:
def websockets_for(resources)
get "/#{resources}", to: "#{resources}#index", as: resources
end
It's basically a helper method I want to make available to generate routes.
I have only found this: https://www.pgrs.net/2007/09/28/add-routes-with-a-rails-plugin-or-gem/
It seems pretty old (from 2007), and I don't think ActionController::Routing is still being used. What's the best way to do this?
Based on Frederick Cheung's comment, I was able to implement this:
require 'action_dispatch/routing'
require 'active_support/concern'
module ActionDispatch::Routing
class Mapper
def websockets_for(resource, &block)
# here, use methods like get, match, etc
get "/#{resources}", to: "#{resources}#index", as: resources
end
end
end
It works! Now, the method websockets_for is available in a Rails app's routes if the gem is installed.
I have written a gem with an install generator. I would like to use this generator to add routes to the config/routes.rb file, much in the same way as the devise gem does by adding devise_for :model_name. Therefore, I need to know how to:
Make a method (like devise_for) available within the scope of routes?
Ok I've figured it out. To add to the routes file you can use the method route in the generator. I have accomplished this by adding the following to my install_generator.rb file:
def setup_routes
route("add_gem_routes")
end
Note that I am in fact calling a method, which can be added to the scope of routes by defining it in the following namespace:
module ActionDispatch::Routing
class Mapper
def add_gem_routes
#routing code...
end
end
end
I use polimorphic_path and it some buggy. This method require some route helper that not defined. How can I define (like regular method) own route helper which will be used like "model_name_path, model_name_url etc"?
This solution worked for me.
Add this code to the end of config/routes.rb file. Make sure to replace MyApp with your application's name.
MyApp::Application.routes.named_routes.module.module_eval do
def model_name_path(*args)
# Your code here
end
def model_name_url(*args)
# Your code here
end
end
MyApp::Application.routes.named_routes.instance_eval do
#helpers += [:model_name_path, :model_name_url]
end
These custom methods will be available in controllers, views and tests.
I know one possible answer for _path, but the same isn't working for me for _url. Anybody know why?
# at the bottom of config/routes.rb
module ActionView::Helpers::UrlHelper
def model_name_path model, args={}
# your implementation
end
end