簡體   English   中英

這條線在Ruby中意味着什么?

[英]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是rails API。 文檔 對於其他一切, 讓我向你推薦一些你會理解的東西

你發布的語法是一種更漂亮的寫作方式

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM