2017-08-16 141 views
1

我是RoR的新手,目前正在完成一些測試任務。 我需要的是從我的形式,命中顯示按鈕的select_tag選擇update_date之後,我想看看在同一頁面上(在「更新容器」的index.html格)從相對應根據所選updated_date分貝信息。我試圖谷歌它以及'stackoverflowed'它,但每次我剛剛陷入更多&更多。從選擇欄選擇選項後更新index.html db值

我index.html.slim:

.container 
    .child-container 
     .show-date 
      = form_tag('/show', 
       method: :get, 
       remote: true, 
       enforce_utf8: false, 
       :'data-update-target' => 'update-container', 
       class: 'select_date') 
       do 
        = collection_select(:id, :id, Dashboard.all, :id, :update_date) 

       = submit_tag 'Show', name: nil 

     #update-container 

我的routes.rb:

Rails.application.routes.draw do 
    resources :dashboards, only: [:index, :show] 
    root to: 'dashboards#index' 
end 

我dashboards_controller.rb:

class DashboardsController < ApplicationController 
    def index 
     @dashboards = Dashboard.all 
    end 
    def show 
     @dashboard = Dashboard.find(params[:id]) 
    end 
end 

從這一點i`ve了「 ActionController :: RoutingError(沒有路由匹配[GET]「/ show」):「。

我將非常感謝任何幫助。提前致謝。

+0

的'''resources'''方法只是ganerates的'''/ dashboards'''和'''/儀表板/:在這種情況下id'''路由。你沒有/ show route(如果你檢查rake路由輸出,你可以看到你的可用路由)。 –

+0

@stockholm_syndrome 試着在你的表單標籤改變'show'到'dashboard_path'(不帶引號) – cnnr

+0

@cnnr,感謝對此事發表評論。然而,使用這種解決方案,我仍然收到「沒有路線匹配......」 –

回答

0

在你的情況,你必須使用下面的代碼routes.rb

get '/show', to: 'dashboard#show' 

如果使用resources :dashboards,這將自動進行顯示路線/dashboards/dashboards/:id

+0

這是簡單的解決方案,它是在我的鼻子下面。謝謝你的幫助! –

0

綜上所述,真的大不了我正是我的問題的標題中提到的點。那麼,這是寫在許多來源,但我偶然發現。

我想提供的解決方案,爲我的作品(也許有助於我這樣的人)。

config/routes.rb

Rails.application.routes.draw do 
    resources :dashboards, only: [:index, :show] 
    get '/show', to: 'dashboards#show' 
    root to: 'dashboards#index' 
end 

app/controllers/dashboards_controller.rb

class DashboardsController < ApplicationController 
    def index 
     @dashboards = Dashboard.all 
    end 

    def show 
     @dashboard = Dashboard.find(params[:id]) 
     respond_to do |format| 
      format.js 
      format.html 
      format.xml 
     end 
    end 
end 

app/views/dashboards/index.html.slim

.container 
    .child-container 
     .show-date 
      = form_tag('/show', 
       method: :get, 
       remote: true, 
       enforce_utf8: false, 
       class: 'select_date') 
       do 
       = select_tag(:id, options_for_select(Dashboard.all.collect{|d| [d.update_date, d.id]}), {include_blank: true}) 
       = submit_tag('Show', name: nil) 

     table[id="update-container"] 

app/views/dashboards/_dashboard.html.slim

thead 
tr 
    th Carousel 
    th Newbie 
    th Other 
tbody 
    tr 
     td #{dashboard.carousel_info} 
     td #{dashboard.newbie} 
     td #{dashboard.others} 

app/views/dashboards/show.js.coffee

$('#update-container').empty() 
$('<%= j(render @dashboard) %>').appendTo("#update-container") 

我真的很感謝他們的幫助!我準備回答任何問題。