我在网站上有一个页面,其中有很多锚链接,例如#menu
| #sauces
等
在页面本身链接工作正常,它的辉煌。 但是,当我在不同的控制器/视图上时,链接不会将我带回主控制器,而是单击锚点。
这是一个锚链接的示例,它位于标题中(位于所有控制器视图上)
<%= link_to '#main', :id => 'menu_link' do %> <li>Menu</li> <% end %>
那是:controller => "main", :action => "index"
当我在另一个控制器中时,例如我的位置控制器,
链接变得像localhost:3000/locations#menu
应该是localhost:3000/#menu
root被设置为转到主控制器和索引操作。
这是我的routes.rb文件
root :to => "main#index" match 'admin', :to => 'access#admin_index' match 'locations', :to => 'ranch_locations#locations' match ':controller(/:action(/:id))(.:format)'
答案 0 :(得分:1)
您需要指定它将转到不同的控制器。
<%= link_to '#main', :controller => "main", :action => "index", :id => 'menu_link' do %>
<li>Menu</li>
<% end %>
答案 1 :(得分:0)
您应该使用root_path(:anchor => :main)
代替"#main"
link_to "Comment wall", profile_path(@profile, :anchor => "wall")
# => <a href="/profiles/1#wall">Comment wall</a>
http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
<强> UPD:强>
<%= link_to '#main', :id => 'menu_link' do %>
应改为
<%= link_to root_path(:anchor => :main), :id => 'menu_link' do %>