我正在使用Rails 4,似乎遇到了我在StackOverflow上发现的常见“点路径”错误。但是,它似乎并不是由多元化错误引起的,而且我对Rails太新了解它为什么会发生这种情况。这是路由以及rake routes
的输出:
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 :(得分: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 :(得分:0)
使用真正的内置路由,更容易扩展和维护:
resources :current_board, controller: :board_members, only: [:index, :show]