2010-11-19 34 views
0

我在現有的Rails控制器(reports_controller)中添加了一個方法來處理超出REST基本範圍的特定操作。讓我們把這一行動「細節」:link_to在Rails中不包含對象的ID 2.3.8

def detail 

@report = Report.find(params[:id]) 

respond_to do |format| 
    format.html # show.html.erb 
    format.xml { render :xml => @report } 
end 

我添加了相應的佈局頁面(detail.html.erb)和路由,以確保我可以從任何地方訪問該頁面。這是我的路線如何:

map.connect "reports/:action", :controller => 'reports', :action => /[a-z]+/i 

現在我可以訪問任何詳細信息頁面。一個例子頁面是這樣的:http://127.0.0.1:3000/reports/detail/8

現在,我試圖創建從主報告索引頁的詳細信息頁面的鏈接,而是使用下面的代碼時:

<%= link_to "Details", {:controller => "reports", :action => "detail", :id => @report }, {:title => "see details for this report"} %> 

鏈接這是創建不包括報告的ID上它看起來像這樣:

http://127.0.0.1:3000/reports/detail 

任何想法,我在做什麼錯?

謝謝!

回答

1

我想你可能會尋找:member

map.resources :reports, :member => { :detail => :get } 

使用link_to

link_to "Detail", detail_report_path(@report) 
+0

也沒有工作。我已經手動創建了這個URL,這是唯一在這一點上工作的...... – wotaskd 2010-11-20 02:40:35

+0

'link_to'會有所不同。我會編輯我的答案。 – 2010-11-20 02:42:21

+0

啊,謝謝!現在錯誤似乎不同,似乎與我的路線有關。我只在我原來的問題上提供了一行,加上你的建議(:member)。這是錯誤:

detail_report_url failed to generate from {:action=>"detail", :controller=>"reports"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["reports", :id, "detail"] - are they all satisfied?
wotaskd 2010-11-20 14:53:53

0

語法錯誤,也許? 嘗試沒有逗號後的權利@report

+0

嗨,這是一個錯字,打字時的問題。即使沒有它也會發生相同的問題 – wotaskd 2010-11-20 01:40:18