[英]What does this line mean in Ruby?
def show
render :text => params.inspect
end
什么是render :text =>
?
什么是render
, :text
和=>
?
它們是標准的紅寶石嗎?
您在該代碼段中使用的語法不僅限於render()
,但它與許多其他Ruby on Rail方法相同。
該方法使用簡化的語法接受哈希映射。
代碼相當於
def show
render({:text => params.inspect})
end
其他包含相同語法的代碼段包括:
def sign
Entry.create(params[:entry])
redirect_to :action => "index"
end
url_for :controller => 'posts', :action => 'recent'
url_for :controller => 'posts', :action => 'index'
url_for :controller => 'posts', :action => 'index', :port=>'8033'
url_for :controller => 'posts', :action => 'show', :id => 10
url_for :controller => 'posts', :user => 'd', :password => '123'
def show
@article = Article.find(params[:id])
fresh_when :etag => @article, :last_modified => @article.created_at.utc, :public => true
end
你發布的語法是一種更漂亮的寫作方式
render({:text => "hello world"})
基本上,您正在調用一個方法,傳入一個Hash對象(它是鍵值對的集合)。 哈希包含1對,其鍵為:text(:表示它是一個名為text的符號),該值為“hello world”字符串
我認為你應該在深入挖掘軌道之前閱讀ruby入門指南。
render:text習慣用於直接將文本呈現給響應,而不需要任何視圖。 它在這里用於調試目的,它將params散列的內容轉儲到響應頁面而不通過頁面視圖。
render :text => "hello world!"
使用狀態代碼200呈現明文“hello world”
這就是:text => ...
意思是參考http://api.rubyonrails.org/classes/ActionController/Base.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.