2011-11-23 23 views
0

在下面的一段代碼中(當然在視圖中)我有<td><%= post.full_hsh %></td>正確顯示了字段post.full_hsh的值,但是url_for給出了一個錯誤:No route matches {:controller=>"posts", :action=>"edit", :id=>nil}Rails:url_to scope

爲什麼post.full_hsh連續修正並且nil在下面的行中?由於

<% @posts.each do |post| %> 
<tr> 
    <td><%= truncate post.body, :length => 100, :separator => ' ' %>...</td> 
    <td><%= post.created_at %></td> 
    <td><%= post.full_hsh %></td>  
    <td><%= url_for :controller => 'posts', :action => 'edit', :id => post.full_hsh %></td> 
</tr> 
<% end %> 

編輯

routes.rb文件:

get "admin/index" 

root :to => 'home#index' 

#access posts via /posts/ab123c or /p/ab123c 
resources :posts, :path => 'p', :except => [:new, :index, :delete] 
resources :posts, :except => [:new, :index, :delete] 

match '/p/:id/delete' => 'posts#delete', :as => 'posts_delete' 
match '/admin' => 'admin#index' 
+0

可您發佈'routes.rb'文件? –

+0

你能否告訴我們'post.full_hsh'可能是什麼樣子? –

+0

這是一個簡單的sha1哈希:'9e60d55edf826483672497b22f9da794571b1275' – pistacchio

回答

0

post.full_hshpost異議的屬性。你可能想:id => post代替

要生成基於對象本身的URL,在這種情況下post,而不是一個對象的屬性(post.full_hsh

+0

嗨。不,我很抱歉,我需要根據Post模型的full_hsh屬性生成自定義鏈接。 – pistacchio