2016-10-15 124 views
1

我試圖使用javascript/AJAX來更改我的控制器定義之一,因此我可以在相同的events#index頁面上顯示即將推出的events和過去events。我發現this SO帖子,表明這個概念並不完全是瘋狂的,但我無法得到它的工作。理想情況下,當您單擊「以前的事件」或「即將發生的事件」時,它會更改控制器定義以顯示相應的events使用Javascript更改Rails控制器實例變量

這裏的HTML/ERB我有我的events#index觀點:

<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 (Not Found)send @ jquery.self-660adc5….js?body=1:10246ajax @ jquery.self-660adc5….js?body=1:9735(anonymous function) @ events: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' 
+0

'url:「<%= url_for(:action =>'update_to_previous_events')'<〜你能用簡單的'update_to_previous_events_path'換掉'url_for'嗎?你有什麼異常 –

+0

@Dimitry_N,在做了這個改變後,我的控制檯顯示這個錯誤:'未捕獲的ReferenceError:update_to_previous_events_path未定義' – Liz

+0

在控制檯中運行'rake_routes'並使用你看到的'update_to_previous_events'的路徑 –

回答

0

作爲評論的形式是小,難以辨認,我喜歡用答案形式寫。 我們已經完成了兩個修復如下。

  1. 使用靜態路徑URL的Ajax請求喜歡

    $.ajax({ url: "/events/update_to_previous_events" }) 
    
  2. 資源之前把定製路由線路:事件

    get 'events/update_to_previous_events' 
    get 'events/update_to_upcoming_events' 
    resources: events 
    

events_controller忽視render :nothing => true,它真的沒有什麼意見。如果你想將數據傳遞到頁面,例如:

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會幫助你,我想。

+0

謝謝你。很有幫助。正如預測的那樣,我仍然在苦苦思索如何部分重新渲染頁面。我從來沒有使用過JSON對象...... – Liz

+0

我很樂意幫助你。如果您對重新渲染有疑問,可以關閉此問題並提出新問題。掛在那裏! – YTorii

相關問題