仍然使用Ruby,Rails和ActiveRecord来获取我的海底......
目前,我正在将闪存通知集成到我的应用程序中(rails 4,ruby 2.1)。我已经开始使用MetaCategory activerecord类了。我正在使用ajax来回移动数据。因此,为了实现flash消息,我构建了创建,更新和销毁动作视图,如下所示:
Create.js.erb / Update.js.erb / Destroy.js.erb:
<% flash.each do |type, message| %>
$('#flash_messages').html("<div class='alert <%= bootstrap_class_for(type) %> alert-dismissible' role='alert'><buttonclass='close' data-dismiss='alert'>×</button><%= message.html_safe%></div>")
<% end %>
我的问题是:在创建和销毁时,我的flash消息会按原样显示。在更新时,它们不会显示。
这是MetaCategories控制器中的创建,更新和销毁活动的代码。我应该指出,在更新的代码中,我尝试了几种不同的策略,它们会产生两种不同的响应。
如果我放弃respond_to做.. render .. end,我收到一个丢失的模板错误(下面的跟踪)。如果我把它放入,我不再收到错误。在这两种情况下,我都看不到flash消息。更让我困惑的是,更新代码是唯一似乎需要respond_to做的代码。毁灭和创造似乎并不关心。 (我是否提到我是铁轨世界的新手?)
无论如何,代码:
def create
# TODO: need uniqueness test for new Metas
if params[:commit]=='Save'
if current_user.try(:admin?)
@meta_category = MetaCategory.create(meta_category_params)
flash[:notice]='Meta category has been created.'
else
MetaCategoryProposedChange.create(new_meta_category: meta_category_params[:name],
proposing_user_id: current_user.id)
flash[:notice]='Your proposed new meta category has been recorded. You will be notified when it has been reviewed.'
end
end
end
def update
# TODO: need uniqueness test for updated Metas
if current_user.try(:admin?)
@meta_category.update(meta_category_params)
flash[:notice]='Meta category has been updated.'
respond_to do |format|
format.json { render 'update.js.erb', json: @meta_category }
end
else
MetaCategoryProposedChange.create(meta_category_id: @meta_category.id,
new_meta_category: meta_category_params[:name],
proposing_user_id: current_user.id)
flash[:notice]='Your proposed change has been recorded. You will be notified when it has been reviewed.'
respond_to do |format|
format.json { render 'update.js.erb', json: @meta_category, :status => 400 }
end
end
end
def destroy
if current_user.try(:admin?)
@meta_category.destroy
flash[:notice]='Meta category has been deleted.'
end
end
追踪:
Started PUT "/meta_categories/95" for 127.0.0.1 at 2014-11-25 11:10:56 -0500
Processing by MetaCategoriesController#update as JSON
Parameters: {"meta_category"=>{"name"=>"#BETABETA"}, "authenticity_token"=>"+Ui6n7tpUPRbGsmFW7RXGbH461Y0UueGSVmi8GlKjqY=", "id"=>"95"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
MetaCategory Load (0.2ms) SELECT `meta_categories`.* FROM `meta_categories` WHERE `meta_categories`.`id` = 95 LIMIT 1
(0.1ms) BEGIN
SQL (0.3ms) UPDATE `meta_categories` SET `name` = '#BETABETA', `updated_at` = '2014-11-25 16:10:56' WHERE `meta_categories`.`id` = 95
(1.4ms) COMMIT
Completed 500 Internal Server Error in 9ms
ActionView::MissingTemplate (Missing template meta_categories/update, application/update with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/Users/cboebel/Documents/Development/Ruby/VerbQ/app/views"
* "/Users/cboebel/.rvm/gems/ruby-2.1.0/gems/devise-3.4.0/app/views"
):
actionpack (4.0.0) lib/action_view/path_set.rb:46:in `find'
actionpack (4.0.0) lib/action_view/lookup_context.rb:115:in `find'
actionpack (4.0.0) lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:35:in `determine_template'
actionpack (4.0.0) lib/action_view/renderer/template_renderer.rb:8:in `render'
actionpack (4.0.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.0) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.0) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/cboebel/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.0.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:433:in `_run__4261403948446499921__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55: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'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57: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'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__1672416812750893188__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/cboebel/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/cboebel/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/cboebel/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Rendered /Users/cboebel/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.7ms)
所以..不确定如何继续。我在这个网站上看到了关于需要告诉如何响应的rails的各种答案,并且当我添加respond_to时我丢失了错误。但令我困扰的是,它似乎没有在update.js.erb中执行我的代码。
谢谢!
答案 0 :(得分:0)
你的答案在这里:
def update
# TODO: need uniqueness test for updated Metas
if current_user.try(:admin?)
@meta_category.update(meta_category_params)
flash[:notice]='Meta category has been updated.'
respond_to do |format|
format.json { render json: @meta_category, :status => 400 }
format.js
end
else
MetaCategoryProposedChange.create(meta_category_id: @meta_category.id,
new_meta_category: meta_category_params[:name],
proposing_user_id: current_user.id)
flash[:notice]='Your proposed change has been recorded. You will be notified when it has been reviewed.'
respond_to do |format|
format.json { render json: @meta_category, :status => 400 }
format.js
end
end