使用Javascript更改Rails控制器实例变量

时间:2016-10-15 22:10:08

标签: javascript ruby-on-rails ajax

我正在尝试使用javascript / AJAX更改我的某个控制器定义,以便在同一events页面上显示即将到来的events和过去events#index。我发现this SO帖子显示这个概念并不完全疯狂,但我无法让它发挥作用。理想情况下,当您单击“之前的事件”或“即将发生的事件”时,它会更改控制器定义以显示相应的events

这是我events#index视图中的html / erb:

<h4 class="text-center">
   <span class="" id="upcomingEvents">Upcoming Events</span> | 
   <span class="linkish" id="previousEvents">Previous Events</span>
</h4>

这是我的javascript(在视图底部放入<script>标记:

<script>
    $("#previousEvents").click(function() {
       $.ajax({ url: "<%= url_for(:action => 'update_to_previous_events') %>" });
       return false;
    });
    $("#upcomingEvents").click(function() {
       $.ajax({ url: "<%= url_for(:action => 'update_to_upcoming_events') %>" });
       return false;
    });
</script>

以下是我events_controller中的相关方法:

  def index
      @events = Event.where("date < ?", Date.today).order('date ASC')
  end

  def update_to_previous_events
    @events = Event.where("date < ?", Date.today).order('date DESC')
    render :nothing => true
  end

  def update_to_upcoming_events
    @events = Event.where("date < ?", Date.today).order('date ASC')
    render :nothing => true
  end

最后这是我的自定义路线:

get 'events/update_to_previous_events'
get 'events/update_to_upcoming_events'

我希望这是一个简单的错误,但我觉得这是一种hackey,所以我担心最坏的情况。谁能看到我哪里出错?

**建议更改为$.ajax({ url: "/events/update_to_previous_events" });后,错误日志现在显示为:

  

jquery.self-660adc5 ... .js?body = 1:10246 GET   http://localhost:3000/events/update_to_previous_events 404(不是   发现)发送@jquery.self-660adc5 ... .js?body = 1:10246ajax @   jquery.self-660adc5 ... .js?body = 1:9735(匿名函数)@   事件:76dispatch @ jquery.self-660adc5 ... .js?body = 1:5227elemData.handle   @ jquery.self-660adc5 ... .js?body = 1:4879

附加信息:服务器错误日志

ActiveRecord::RecordNotFound - Couldn't find Event with 'id'=update_to_previous_events:
  activerecord (4.2.5) lib/active_record/core.rb:155:in `find'
  app/controllers/events_controller.rb:54:in `show'
  actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/elizabethbayardelle/.rbenv/versions/2.2.4/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

1 个答案:

答案 0 :(得分:0)

由于评论表格小而且难以辨认,我想以答复形式写。 我们做了两个修复,如下所示。

  1. 将静态路径用作ajax请求的URL,如

    $.ajax({ url: "/events/update_to_previous_events" })
    
  2. 在资源:事件

    之前放置自定义路线
    get 'events/update_to_previous_events'
    get 'events/update_to_upcoming_events'
    resources: events
    
  3. 我忽略了render :nothing => true中的events_controller,它确实对视图无效。如果要将数据传递给页面,例如:

    events_controller:

    def update_to_previous_events
      @events = Event.where("date < ?", Date.today).order('date DESC')
      render json: { :events => @events }
    end
    

    的javascript:

    $("#previousEvents").click(function() {
       $.ajax({ 
         url: "/events/update_to_previous_events",
         success: function(data) { 
           console.log(data.events)
           ...or your codes for listing events...
         }
       });
       return false;
    });
    

    如果您不确定如何部分重新呈现页面,我认为this post可以帮助您。