2012-11-02 46 views
0

我想了解如何在視圖中使用超鏈接來訪問自定義控制器方法。我在我的控制器中有以下代碼,並且我想在視圖中使用link_to命令來訪問它。我猜在路線文件中我需要做些什麼來使launch_build_file方法有效?我應該在視圖中列出哪些代碼來觸發launch_build_file方法?如何使用自定義控制器方法?

class ReportsController < ApplicationController 

    def index 
    end 

    def launch_build_file 
    Process.spawn("ruby #{Rails.root}/lib/build.rb") 
    end 

end 
+1

? – Amar

+0

我在學習Rails方面很新。我確信在Rails中有這樣一種更優雅的方式 - 但作爲第一步,我希望能夠通過單擊View in Rails中的鏈接來運行現有的Ruby文件。 換句話說,我不想在命令提示符下鍵入'ruby build.rb',我想通過單擊視圖中的鏈接觸發文件運行。 – 2scottish

+0

好的查看我的答案 – Amar

回答

1

在你的路由文件假設你有報道資源,如果沒有,那麼你可以你爲什麼產卵紅寶石過程中使用命名路由

resources :reports do 

collection do 
    get :launch_build_file 
end 


end 
#or 
match '/reports/launch_build_file' => "reports#launch_build_file", :as => 'launch_build_file' 

#If it's collection route 
link_to launch_build_file_reports_path 
#or 
link_to launch_build_file_path 
+0

你打我時間:) – HungryCoder

+0

謝謝。我嘗試使用收集建議,現在我看到一個錯誤消息: '缺少模板報告/ launch_build_file,application/launch_build_file {:locale => [:en],:formats => [:html],:handlers = > [:erb,:builder,:coffee]}。搜索:*「C:/ rails_projects/av_reports_temp/app/views」' 我在做什麼錯? – 2scottish

+0

'render:nothing => true'或'redirect_to'某頁 – Amar

1
link_to "foo", :controller => :reports, :action => :launch_build_file 

或者你可以做一個名爲路線,並用它來獲取URL。

+0

謝謝。現在我開始明白一點點了。 – 2scottish

相關問題