2016-05-13 49 views
0

我是一個新手到鐵路,並嘗試建立我的第一個網站,但面臨一個索引頁面link_to問題。鏈接重定向到/recipes.1而不是/ recipes/1。 當我嘗試/食譜/ 1時,顯示頁面工作。問題link_to在索引

Index.html.erb

<% provide(:title, "Recipe") %> 
<% @recipes.each do |recipe| %> 
    <%= link_to recipe.label, recipe%> 
<%end%> 

route.db

get  'recipe'  => 'recipes#show' 
get  'recipe'  => 'recipes#new' 
post  'recipe'  => 'recipes#create' 
resources :users 
resources :recipes 

recipes_controller.rb

def index 
    @recipes = Recipe.all 
end 

def show 
    @recipe = Recipe.find(params[:id]) 
end 

回答

0

試試這個代碼

<% @recipes.each do |recipe| %> 
    <%= link_to recipe.label, recipe_url(recipe) %> 
<%end%> 
1

routes.rb中刪除以下路線:

get  'recipe'  => 'recipes#show' 
get  'recipe'  => 'recipes#new' 
post  'recipe'  => 'recipes#create' 

上述路線不需要的,因爲resources :recipes產生所有這些途徑爲您服務。

希望它有幫助!改用的

<%= link_to recipe.label, recipe%> 
+0

謝謝,這是問題,我已經刪除它們,它的工作。 –

+0

很高興它解決了你的問題! – dp7

0

<%=link_to recipe.label, recipe_path(recipe)%> 

您已經定義了兩次表演路徑。 1.手動定義的路徑

get  'recipe'  => 'recipes#show' 

2.通過

resources :recipes 

如果你這樣做rake routes,那麼你將第一個選項創建get方法途徑與/recipe URL並追加參數,將其這導致/recipes.1路徑。

還軌讀取從上到下方法的路線文件。由於使用show方法的第一條路線。