所以我有一个名为guests_controller.rb的控制器,如下所示:
class GuestsController < ApplicationController
include Data
def index
display_data("db")
display_data("session")
end
def new
@guest = Guest.new
end
def create
case params[:option]
when '0'
logger.debug 'Salvestame andmebaasi'
save_data("db")
when '1'
logger.debug 'Salvestame sessiooni'
save_data("session")
end
end
private
def guest_params
params.require(:guest).permit(:name, :text)
end
end
而且我在控制器/ concens /中有一个名为data.rb的问题:
module Data
extend ActiveSupport:Concern
def save_data(save_method)
case save_method
when "db"
@guest = Guest.new(guest_params)
if @guest.save
flash[:success] = "New guest entry created!"
redirect_to guests_path
else
render 'new'
end
when "session"
name = params[:guest][:name].to_s
text = params[:guest][:text].to_s
@guests_session = session[:guests]
if session[:guests].nil?
@guests_session = [{:name => 'temp', :text => 'temp'}]
else
logger.debug @guests_session
@guests_session.push({:name => name, :text => text})
end
session[:guests] = @guests_session
redirect_to guests_path
end
end
def display_data(display_method)
case display_method
when "db"
@guests = Guest.all
when "session"
if session[:guests].nil?
@guests_session = [{:name => 'temp', :text => 'temp'}]
else
#@guests_session = [{:name => 'temp', :text => 'temp'}]
@guests_session = session[:guests]
end
end
end
end
现在,当我打开相应的视图时,我收到此错误:
GuestsController #index中的TypeError 错误的参数类型类(预期的模块)
使用stacktrace:
app/controllers/guests_controller.rb:2:in `include'
app/controllers/guests_controller.rb:2:in `<class:GuestsController>'
app/controllers/guests_controller.rb:1:in `<top (required)>'
activesupport (4.1.5) lib/active_support/dependencies.rb:443:in `load'
activesupport (4.1.5) lib/active_support/dependencies.rb:443:in `block in load_file'
activesupport (4.1.5) lib/active_support/dependencies.rb:633:in `new_constants_in'
activesupport (4.1.5) lib/active_support/dependencies.rb:442:in `load_file'
activesupport (4.1.5) lib/active_support/dependencies.rb:342:in `require_or_load'
activesupport (4.1.5) lib/active_support/dependencies.rb:480:in `load_missing_constant'
activesupport (4.1.5) lib/active_support/dependencies.rb:180:in `const_missing'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `const_get'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:238:in `block in constantize'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `each'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `inject'
activesupport (4.1.5) lib/active_support/inflector/methods.rb:236:in `constantize'
activesupport (4.1.5) lib/active_support/dependencies.rb:552:in `get'
activesupport (4.1.5) lib/active_support/dependencies.rb:583:in `constantize'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:78:in `controller_reference'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:68:in `controller'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:46:in `call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
passenger (4.0.50) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.50) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.50) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.50) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
我基本上只是想学习如何使用控制器的问题。我想分离一大堆我的代码并将其放入相应的关注点以保持控制器的美观和清洁。
谷歌搜索或搜索SO以寻找控制器的问题时,没有什么可以找到的,所以我提出这个新问题希望得到帮助。
那么如何解决这个问题以及我做错了什么?
答案 0 :(得分:8)
您遇到名称冲突 - 核心Ruby中存在Data
类:link here。这意味着Rails不会启动const_missing,并且永远不会加载您的顾虑。将您的关注重命名为DateModule或类似的。
答案 1 :(得分:1)
你在数据问题上做了一个简单的拼写错误 - 它应该是extend ActiveSupport::Concern
(注意双冒号,这是Ruby中的命名空间分隔符)。