2013-12-20 64 views
0

我使用Rails 4,似乎遇到了我在StackOverflow上發現的常見「點路由」錯誤。但是,它似乎並不是由於多元化錯誤導致的,而且我對Rails也不太瞭解它爲何會發生。下面是與rake routes輸出沿着路由:Rails Path Helper生成Dot Route而不是Slash

match '/current_board', to: 'board_members#index', via: 'get' 
match '/current_board/:id', to: 'board_members#show', via: 'get' 

      Prefix Verb URI Pattern      Controller#Action 
    board_members GET /board_members(.:format)   board_members#index 
        POST /board_members(.:format)   board_members#create 
new_board_member GET /board_members/new(.:format)  board_members#new 
edit_board_member GET /board_members/:id/edit(.:format) board_members#edit 
    board_member GET /board_members/:id(.:format)  board_members#show 
        PATCH /board_members/:id(.:format)  board_members#update 
        PUT /board_members/:id(.:format)  board_members#update 
        DELETE /board_members/:id(.:format)  board_members#destroy 
    current_board GET /current_board(.:format)   board_members#index 
        GET /current_board/:id(.:format)  board_members#show 

然而,當我做到以下幾點:

<% @board_members[0..5].each do |board_member| %> 
    <div class="col-xs-4 col-md-2"> 
     <a href="<%= current_board_path(board_member) %>"> 
      <%= image_tag(board_member.photo_url, alt: "Photo of #{board_member.name}", class: 'center-block board-thumbnail img-rounded') %> 
      <div class="caption"> 
       <h4 class="text-center"><%= board_member.name %></h4> 
       <p class="text-center"><%= board_member.position %></p> 
      </div> 
     </a> 
    </div> 
<% end %> 

我最終得到http://localhost:3000/current_board.1等的路徑。

回答

0

真正使用內置的路由,更易於擴展和維護:

resources :current_board, controller: :board_members, only: [:index, :show] 
+0

這個工作完美,比其他解決方案更清潔。一個警告是我現在必須使用'current_board_index_path'而不是'current_board_path'作爲'index',但它的工作原理!謝謝! – sethfri

2

首先你應該使用

get '/current_board', to: 'board_members#index', :as => 'current_boards' 
get '/current_board/:id', to: 'board_members#show', :as => 'current_board' 

這使得它更容易做解決途徑。但它很醜。

和使用的 「A」 使用的link_to助手與塊

<%= link_to current_board_path(board_member) do %> 
    <%= image_tag(board_member.photo_url, alt: "Photo of #{board_member.name}", class: 'center-block board-thumbnail img-rounded') %> 
    <div class="caption"> 
    <h4 class="text-center"><%= board_member.name %></h4> 
    <p class="text-center"><%= board_member.position %></p> 
    </div> 
<% end %> 

它應該工作。試一試。

+1

另一種解決方案是乾淨多了,不過還是謝謝你讓我知道與塊的link_to幫手! +1 – sethfri

相關問題