2010-11-19 37 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