2015-02-23 81 views
0

我有我的搜索設置像這樣。如何使用搜索功能更新FullCalendar事件?

# Viewings controller 
    def index 
     @rmquery = params[:search] 
    if params[:search] 
     @viewings = Viewing.search(params[:search]) 
    else 
     @viewings = Viewing.all 
    end 
    end 

class Viewing < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :room 

    def self.search(query) 
    self.where("room_id like ?", "%#{query}%") 
    end 
end 

,我有我的(收看)index.jbuilder如下:

json.array!(@viewings) do |viewing| 

    json.title "viewing" 
    json.start viewing.start_time 
    json.end viewing.end_time 
    json.url viewing_url(viewing, format: :html) 
end 

我想我viewings.json反映搜索結果,但它只是似乎顯示所有的觀看,即使搜索工作在「列表視圖」

I已經嘗試了一些沿線:

@viewings = Viewing.where(room_id: @rmquery) 
json.array!(@viewings) do |viewing| 

    json.title "viewing" 
    json.start viewing.start_time 
    json.end viewing.end_time 
    json.url viewing_url(viewing, format: :html) 
end 

但它似乎並沒有通過查詢。

哦,我的索引視圖:

<p id="notice"><%= notice %></p> 


<%= form_tag(viewings_path, :method => "get", id: "search-form") do %> 
    <%= text_field_tag :search, params[:search], placeholder: "Search rooms" %> 
    <%= submit_tag "Search", :name => nil %> 
<% end %> 

<div id="calendar"> </div> 

<h1>Listing Viewings</h1> 

<table> 
    <thead> 
    <tr> 
     <th>Room</th> 
     <th>Customer</th> 
     <th>Start time</th> 
     <th>End time</th> 
     <th>Notes</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @viewings.each do |viewing| %> 
     <tr> 
     <td><%= viewing.room_id %></td> 
     <td><%= viewing.customer_id %></td> 
     <td><%= viewing.start_time %></td> 
     <td><%= viewing.end_time %></td> 
     <td><%= viewing.notes %></td> 
     <td><%= link_to 'Show', viewing %></td> 
     <td><%= link_to 'Edit', edit_viewing_path(viewing) %></td> 
     <td><%= link_to 'Destroy', viewing, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New Viewing', new_viewing_path %> 

代碼日曆:

$(document).ready(function() { 

    // page is now ready, initialize the calendar... 

    $('#calendar').fullCalendar({ 
     events: '/viewings.json', 
     defaultView: 'agendaWeek', 
     slotMinutes: 60, 
    }) 

}); 

服務器日誌中搜索

C:\Sites\weddings 
=> Booting WEBrick 
=> Rails 4.2.0 application starting in development on http://localhost:3000 
=> Run `rails server -h` for more startup options 
=> Ctrl-C to shutdown server 
[2015-02-24 22:40:04] INFO WEBrick 1.3.1 
[2015-02-24 22:40:04] INFO ruby 1.9.3 (2014-02-24) [i386-mingw32] 
[2015-02-24 22:40:04] INFO WEBrick::HTTPServer#start: pid=8260 port=3000 


Started GET "/viewings?utf8=%E2%9C%93&search=1" for ::1 at 2015-02-24 22:40:14 + 
0000 
    ←[1m←[36mActiveRecord::SchemaMigration Load (1.0ms)←[0m ←[1mSELECT "schema_mi 
grations".* FROM "schema_migrations"←[0m 
Processing by ViewingsController#index as HTML 
    Parameters: {"utf8"=>"V", "search"=>"1"} 
    ←[1m←[35mViewing Load (1.0ms)←[0m SELECT "viewings".* FROM "viewings" WHERE (
room_id like '%1%') 
    Rendered viewings/index.html.erb within layouts/application (52.0ms) 
    ←[1m←[36mUser Load (1.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "user 
s"."id" = ? ORDER BY "users"."id" ASC LIMIT 1←[0m [["id", 1]] 
Completed 200 OK in 1216ms (Views: 1100.8ms | ActiveRecord: 4.0ms) 


Started GET "/assets/fullcalendar-6bee23c458d42683dba9159c94f7547f.css?body=1" f 
or ::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/application-4a810b319e4a8061f2f2b27a579eadc1.css?body=1" fo 
r ::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/forms-2477fa28d6be1997f320e98a5ceee0f6.css?body=1" for ::1 
at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/index-divs-e5867ca540c81afa8a01e2daca2920c5.css?body=1" for 
::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/owl.carousel-688ba12698d3c02b724b8a26bc3b1bd0.css?body=1" f 
or ::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/owl.theme-f91781dace0c67e4d6a689ddd73d65b7.css?body=1" for 
::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/owl.transitions-1b9d47a45c85e9fa3f2f990b6d59519a.css?body=1 
" for ::1 at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/pages-8450695447adc4c25ef72b73f84e12b5.css?body=1" for ::1 
at 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:15] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/show-bd6702d041298d2548881c1db5e4e9a7.css?body=1" for ::1 a 
t 2015-02-24 22:40:15 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/viewings-64e62ddc273c2f5847f30d698ca14b67.css?body=1" for : 
:1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/moment-66988874bf5714cb1e4a6f53d733b6e1.js?body=1" for ::1 
at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/jquery-87424c3c19e96d4fb033c10ebe21ec40.js?body=1" for ::1 
at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/jquery_ujs-e27bd20a10d28155845a22d71ef94f2f.js?body=1" for 
::1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/turbolinks-da8ddad9c2c5483d4c5c30c1ce325253.js?body=1" for 
::1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/fullcalendar-f09a2cee6bd3f7655c47cf7eab2b52b9.js?body=1" fo 
r ::1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/owl.carousel.min-7972660fd0b56886b322fbde7ad2f1f7.js?body=1 
" for ::1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/pages-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1 a 
t 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/rmcats-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1 
at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/rooms-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for ::1 a 
t 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/viewings-fcec5b5a277ac7c20cc9f45a209a3bcd.js?body=1" for :: 
1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/assets/application-5fac6df9a34c6823e27885b0701b0037.js?body=1" for 
::1 at 2015-02-24 22:40:16 +0000 
[2015-02-24 22:40:16] WARN Could not determine content-length of response body. 
Set content-length of the response or set Response#chunked = true 


Started GET "/viewings?start=2015-02-22&end=2015-03-01&_=1424817616261" for ::1 
at 2015-02-24 22:40:17 +0000 
Processing by ViewingsController#index as JSON 
    Parameters: {"start"=>"2015-02-22", "end"=>"2015-03-01", "_"=>"1424817616261"} 

    ←[1m←[35mViewing Load (1.0ms)←[0m SELECT "viewings".* FROM "viewings" 
    Rendered viewings/index.json.jbuilder (30.1ms) 
Completed 200 OK in 65ms (Views: 59.3ms | ActiveRecord: 1.0ms) 

我不知道所有這些"Could not determine content-length of response body"消息是,但我認爲上面的最後3行似乎是什麼導致它顯示日曆中的所有查看。

+0

你正在告訴它'呈現索引'。所以它轉到'index'動作並呈現它。不知道你想要什麼結果。請說明你的控制器代碼是如何佈置的。 – Beartech 2015-02-23 16:10:52

+0

你試圖達成什麼樣的整體觀點?你在github上有這個代碼嗎?什麼是你的視圖代碼調用部分?你不能在同一個視圖中使用兩個不同的方法來使用同一個變量'@ viewings'。 – Beartech 2015-02-23 16:13:18

+0

如果有任何幫助,我可以推送給github。在def index和def rmselect中進行@viewing是我教過如何實現搜索的方式,但正如你可能知道的那樣,我對ruby真的很陌生! – Mitch 2015-02-23 16:17:42

回答

0

你可能需要配合搜索功能調用events.js.coffee日曆功能:

$(document).ready -> 
    $("#calendar").fullCalendar(
    events: '/events.json' 
) 

不知道如何做到這一點,但要觸發每個搜索後這個功能,因爲您正在獲取正確過濾的列表,但日曆不會使用當前的事件列表重新加載。

我開始想,也許你會更好,只需要顯示一切的主日曆,而不是搜索功能,使用列出每個房間的鏈接的下拉菜單。然後,您可以只窩您的路線:

在配置

/routes.rb中

...

resources rooms do 
    resources events 
end 

這樣,你的鏈接到每個房間都會去那個房間的日曆像

路線
localhost:3000/room1/events 

因爲事件已經屬於房間。在你的房間控制器可以讓#show行動使用房間ID查找事件:

def show 
    @room_events = Room.where(id: room_params[:id]).events 
end 

然後在/views/rooms/show你會把日曆代碼。

的選擇房間的下拉會從查詢

@rooms = Room.all 

建在飛,然後只用一個下拉菜單中.erb讓每個房間的鏈接。

編輯: 我不熟悉jbuilder,所以我的回答可能沒有任何幫助。您是否打算在已經加載一次後更新索引頁? 我仍然認爲你的問題是你正在定義變量@viewings兩次,一次在部分中,一次在索引中。最後一次調用是索引,這是顯示時的最終值。


好了,看着你的日曆代碼:

$(document).ready(function() { 

// page is now ready, initialize the calendar... 

$('#calendar').fullCalendar({ 
    events: '/viewings.json', 
    defaultView: 'agendaWeek', 
    slotMinutes: 60, 
}) 

}); 

我想知道如果這是你的問題是,也許你需要從/viewings.json加載更新事件。因此,要麼使用AJAX來請求過濾的事件查詢,然後從.js.erb部分和.html.erb部分更新日曆?如果日曆基於表並且您正在使用Jquery,則可以使用Jquery內置的過濾器函數。

或...

您可以使用CSS。我不擅長CSS,但我敢肯定你可以將所有事件設置爲可見,並使用搜索功能作爲過濾器將所有內容的可見性更改爲不可見,除了那些進入您要過濾的房間的內容。


這與有限的信息黑暗中刺下去,但請嘗試更改房間有限收看您的實例變量從

@viewings 

喜歡的東西:

@room_viewings 

這樣你打電話時不會跺腳

render index