簡體   English   中英

Rails在URL中添加id作為參數

[英]Rails adding id as a param in url

我敢肯定這與我的N00b語法有關,但是我很難弄清它是什么...

我通過遍歷子頁面表中的項目並為返回的每個項目創建一個鏈接來創建菜單,就像這樣:

<% @subpages.each do |menu| %>
<%= link_to(menu.name, {:controller => 'public', :action => "page", :id => menu.permalink }, :class => "show action footer-link") %>
<% end %>

這在我網站的首頁上運行良好,但是如果您訪問其中一個子頁面,那么說關於我們頁面:

http://localhost:3000/public/page/about-us

然后嘗試再次使用菜單訪問“聯系我們”,而不是像我期望的那樣將您帶到此鏈接:

http://localhost:3000/public/page/contact-us

它帶您到此鏈接:

http://localhost:3000/public/page/about-us?id=contact-us

我在這里犯了什么男生錯誤?

提前致謝。

編輯:我的路線

  root :to => "public#index"
  get 'admin', :to => 'access#menu'  
  get 'public/show/:permalink', :to => 'public#show'
  get 'public/page/:permalink', :to => 'public#page'

試試這個:

<% @subpages.each do |menu| %>
  <%= link_to(menu.name, page_public_path(menu.permalink), :class => "show action footer-link") %>
<% end %>

謝謝

我通過將:permalink參數傳遞給控制器​​而不是ID來使其正常工作

<% @subpages.each do |menu| %>
    <%= link_to(menu.name, { :action => "page", :permalink => menu.permalink }, :class => "show action footer-link") %>
<% end %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM