2017-08-16 76 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") 

我真的很感谢他们的帮助!我准备回答任何问题。