[英]Rails render doesn't work
我正在嘗試從Rails控制器返回一個json值,但是奇怪的是它不起作用,並且總是需要html格式。 這是我的控制器方法:
def result
@command = params[:output]
@result = []
IO.popen("free -m") { |f| f.each { |e| @result << e } }
rescue Errno::ENOENT
@result << "No command found"
render json: @result.to_json
end
嘗試訪問此頁面后,出現以下錯誤:MainController#result作為HTML處理
ActionView::MissingTemplate (Missing template main/result, application/result with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :haml]}. Searched in:
* "/home/vyivrain/Documents/RubyProjects/Diploma/app/views"
為什么只需要html格式。 我也嘗試執行response_to或不呈現任何內容或呈現純文本,但結果相同。 順便說一下,我的rails版本是4.1.5。 所以有點奇怪。 謝謝你的答案。
更改為此:
def result
begin
@command = params[:output]
@result = []
IO.popen("free -m") { |f| f.each { |e| @result << e } }
rescue Errno::ENOENT
@result << "No command found"
end
render json: @result.to_json
end
當您單擊重定向到result
操作的鏈接時,嘗試指定:remote => true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.