2013-11-20 44 views
0

下面是我的路線文件中定義的路線:可以直接導航到路由路徑,但不能通過單擊鏈接導航?

#routes for colleges 
get "colleges/universityofconnecticut" 
get "colleges/ferrisstateuniversity" 

#routes for states 
get "pages/connecticut" 
get "pages/michigan" 

#root route 
root :to => "pages#home" 

這裏是我創建了一個下拉菜單。請注意,使用路線:

<form> 
<select name="URL" onchange="window.location.href= this.form.URL.options[this.form.URL.selectedIndex].value"> 
    <option><%= link_to 'Connecticut', pages_connecticut_path %></option> 
    <option> <%= link_to 'Michigan', pages_michigan_path %></option> 
</select> 
</form> 

當我點擊下拉鍊接,我收到在瀏覽器中的錯誤是「沒有路由匹配/密歇根州」和「無路由匹配/康涅狄格」。但是,我可以輸入到我的瀏覽器本地主機:3000 /頁/康涅狄格,它會工作。

任何想法爲什麼會發生這種情況?

編輯:這裏是結果「耙路線」

colleges_universityofconnecticut GET /colleges/universityofconnecticut(.:format) {:controller=>"colleges", :action=>"universityofconnecticut"} 
    colleges_ferrisstateuniversity GET /colleges/ferrisstateuniversity(.:format) {:controller=>"colleges", :action=>"ferrisstateuniversity"} 
       pages_connecticut GET /pages/connecticut(.:format)    {:controller=>"pages", :action=>"connecticut"} 
     pages_michigan GET /pages/michigan(.:format)     {:controller=>"pages", :action=>"michigan"} 
         root  /(.:format)         {:controller=>"pages", :action=>"home"} 
+0

你能粘貼'耙routes'的結果呢? – zeroed

+0

剛做過@zeroed –

+1

看到這個問題:http://stackoverflow.com/questions/2944977/how-to-redirect-to-another-page-using-a-select-menu-and-link-to-ruby- on-rail – siekfried

回答

1

你創建你的<option>內的<a>標籤,我相信。檢查輸出到瀏覽器的HTML。 HTML應該看起來像:

<option value="/pages/connecticut">Connecticut</option> 

你可能會得到一些不同的東西。一種解決方案是:

<option value=<%= pages_connecticut_path %>>Connecticut</option> 

這應該創建正確的HTML。可能有一些具體的方法可以在Rails中使用,我還沒有看到使用form_forsimple_form。另請參見本:

How to redirect to another page using a select menu and link_to - Ruby on Rails