我将我的rails版本从3.2.13升级到4.0.0,我也升级了我的宝石。当我从本地服务器运行应用程序时,这是我得到的错误:
于2016-06-24 11:54:37 +0530开始为127.0.0.1获取“/signup.html”
ActionController :: RoutingError - 未定义的局部变量或方法
inherit_resources' for AccountsController:Class: actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:69:in
救援控制员' actionpack(4.0.0)lib / action_dispatch / routing / route_set.rb:64:incontroller' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:44:in
调用'actionpack (4.0.0)lib / action_dispatch / journey / router.rb:71:在block in call'
调用' actionpack(4.0.0)lib / action_dispatch / routing / route_set.rb:655:in
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:incall' meta_request (0.4.0) lib/meta_request/middlewares/app_request_handler.rb:13:in
称之为'
meta_request(0.4.0) lib / meta_request / middlewares / meta_request_handler.rb:13:在call'
块中呼叫'看守 (1.0.6)lib / warden / manager.rb:34:in
warden (1.0.6) lib/warden/manager.rb:35:incall' client_side_validations (3.0.12) lib/client_side_validations/middleware.rb:17:in
call'rack (1.5.5)lib / rack / etag.rb:23:incall' rack (1.5.5) lib/rack/conditionalget.rb:25:in
call'rack(1.5.5) lib / rack / head.rb:11:incall' remotipart (1.2.1) lib/remotipart/middleware.rb:27:in
call'actionpack(4.0.0) lib / action_dispatch / middleware / params_parser.rb:27:在call'
调用'机架(1.5.5)lib / rack / session / abstract / id.rb:225:在
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:incontext' rack (1.5.5) lib/rack/session/abstract/id.rb:220:in
调用'
actionpack(4.0.0)lib / action_dispatch / middleware / cookies.rb:486:incall' activerecord (4.0.0) lib/active_record/query_cache.rb:36:in
调用'activerecord(4.0.0) LIB / active_record / connection_adapters /抽象/ connection_pool.rb:626:在call' activerecord (4.0.0) lib/active_record/migration.rb:369:in
调用'actionpack(4.0.0) lib / action_dispatch / middleware / callbacks.rb:29:inblock in call'
_ run__139117187983862327__call__callbacks'activesupport(4.0.0) lib / active_support / callbacks.rb:80:in
activesupport (4.0.0) lib/active_support/callbacks.rb:373:inrun_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in
call'
rails-dev-tweaks(1.2.0) lib / rails_dev_tweaks / granular_autoload / middleware.rb:36:incall'
call'airbrake(4.3.7)lib / airbrake / rails / middleware.rb:13:in:
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:incall' better_errors (2.1.1) lib/better_errors/middleware.rb:84:in
protected_app_call'beperior_errors(2.1.1) lib / better_errors / middleware.rb:79:inbetter_errors_call'
call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in
actionpack(4.0.0) lib / action_dispatch / middleware / debug_exceptions.rb:17:incall'
call' meta_request(0.4.0)lib / meta_request / middlewares / headers.rb:16:in
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:incall' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in
称之为'
railties(4.0.0)lib / rails / rack / logger.rb:38:incall_app' railties (4.0.0) lib/rails/rack/logger.rb:21:in
阻止呼叫'activesupport (4.0.0)lib / active_support / tagged_logging.rb:67:在block in tagged'
标记为'activesupport(4.0.0) lib / active_support / tagged_logging.rb:67:在
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:intagged' railties (4.0.0) lib/rails/rack/logger.rb:21:in
调用'actionpack(4.0.0) lib / action_dispatch / middleware / request_id.rb:21:incall' rack (1.5.5) lib/rack/methodoverride.rb:21:in
call'rack(1.5.5) lib / rack / runtime.rb:17:incall' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in
call'rack (1.5.5)lib / rack / lock.rb:17:incall' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in
调用'airbrake (4.3.7)lib / airbrake / user_informer.rb:16:在_call' airbrake (4.3.7) lib/airbrake/user_informer.rb:12:in
调用'railties(4.0.0) lib / rails / engine.rb:511:在call' railties (4.0.0) lib/rails/application.rb:97:in
呼叫'机架(1.5.5) lib / rack / content_length.rb:14:在pre_process中的call' thin (1.5.1) lib/thin/connection.rb:81:in
块'瘦(1.5.1) lib / thin / connection.rb:79:在pre_process' thin (1.5.1) lib/thin/connection.rb:54:in
进程'瘦(1.5.1) lib / thin / connection.rb:39:在receive_data' eventmachine (1.0.9) lib/eventmachine.rb:193:in
运行'瘦'(1.5.1) lib / thin / backends / base.rb:63:instart' thin (1.5.1) lib/thin/server.rb:159:in
start'rack(1.5.5) lib / rack / handler / thin.rb:16:inrun' rack (1.5.5) lib/rack/server.rb:264:in
start'railties(4.0.0) lib / rails / commands / server.rb:84:在'railties中的start' railties (4.0.0) lib/rails/commands.rb:78:in
块中 (4.0.0)lib / rails / commands.rb:73:在exec_app_rails中的<top (required)>' railties (4.0.0) lib/rails/app_rails_loader.rb:43:in
块中 railties(4.0.0)lib / rails / app_rails_loader.rb:32:inexec_app_rails' railties (4.0.0) lib/rails/cli.rb:6:in
'railties (4.0.0)bin / rails:9:in<top (required)>' () home/user/.rvm/gems/ruby-2.3.0@project/bin/rails:23:in
'() home/user/.rvm/gems/ruby-2.3.0@project/bin/ruby_executable_hooks:15:在 `'
在我以前的版本中,routes文件是这样的:
match '/signup' => 'accounts#new', :plan => 'year'
升级后,我将其更改为:
get '/signup' => 'accounts#new', :plan => 'year'
“匹配”被所有路线的“获取”取代。
这是我们使用inherited_resources的accounts_controller。
class AccountsController < ApplicationController
inherit_resources
ssl_required :new, :create
before_filter :authenticate_user!, :except => [:new, :create]
...................
end
我无法追踪他们使用的原因,也无法在Gemfile中找到它。请帮我修复此错误。
答案 0 :(得分:0)
inherit_resources旨在使使用REST约定的控制器变干。它是一个已弃用的gem,它不再在Rails 4中维护。inherit_resources本质上将从InheritedResources :: Base继承,它具有预先定义的REST方法。
相反,您可以像平常一样删除它并在控制器中定义路线:
class AccountsController < ApplicationController
ssl_required :new, :create
before_filter :authenticate_user!, :except => [:new, :create]
def index
@accounts = Accounts.all
end
def show
@account = Account.find(params[:id])
end
def new
@account = Account.new
end
def create
@account = Account.new(account_params)
if @account.save
redirect_to @account
else
render 'new'
end
end
def edit
@account = Account.find(params[:id])
end
def update
@account = Account.find(params[:id])
if @account.update_attributes(account_params)
flash[:notice] = "Successfully updated account!"
redirect_to @account
else
render 'edit'
end
end
def destroy
@account = Account.find(params[:id])
@account.destroy
redirect_to accounts_path
end
private
def account_params
params.require(:account).permit(:name, :email, etc) # Look up strong params if you are unfamiliar. The name, email, etc. are going to be the only permitted params passed in the methods above as a safety feature so you have to permit the fields being created here.
end
这是继承资源添加到控制器中的概要,目的是拥有“胖模型和瘦控制器”。你可以找到其他类似的东西,但在我看来,我只是为了可读性而自己添加方法。
编辑:它正在添加这些方法定义,但根据您的应用程序将定义这些方法中的内容。这将是我最好的粗略猜测。 Rails 4中的新功能也是增加了强大的参数。在创建和更新操作中,您会看到从私有方法调用account_params,而不是使用方法内的原始参数进行更新。