简体   繁体   English

这条线在Ruby中意味着什么?

[英]What does this line mean in Ruby?

def show
  render :text => params.inspect
end

What is render :text => ? 什么是render :text =>

What is render , :text , and the => ? 什么是render:text=>
Are they standard ruby? 它们是标准的红宝石吗?

The syntax you see used in that code snippet is not limited to render() , but it is common with many other Ruby on Rail methods. 您在该代码段中使用的语法不仅限于render() ,但它与许多其他Ruby on Rail方法相同。

The method is accepting a hash map, using a simplified syntax. 该方法使用简化的语法接受哈希映射。
The code is equivalent to 代码相当于

def show
  render({:text => params.inspect})
end

Other code snippets that include the same syntax are: 其他包含相同语法的代码段包括:

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 is rails API. render是rails API。 See doc . 文档 For everything else, let me recommend you something and you will understand. 对于其他一切, 让我向你推荐一些你会理解的东西

the syntax you have posted is a prettier way of writing 你发布的语法是一种更漂亮的写作方式

render({:text => "hello world"})

basically, you are calling a method, passing in a Hash object (which is a collection of key value pairs). 基本上,您正在调用一个方法,传入一个Hash对象(它是键值对的集合)。 The hash contains 1 pair, with a key of :text (: indicating it is a symbol called text), the value being a string of "hello world" 哈希包含1对,其键为:text(:表示它是一个名为text的符号),该值为“hello world”字符串

I think you should really be reading the ruby getting started guides before digging too deep in to rails. 我认为你应该在深入挖掘轨道之前阅读ruby入门指南。

The render :text idiom is for rendering text directly to the response, without any view. render:text习惯用于直接将文本呈现给响应,而不需要任何视图。 It's used here for debugging purposes, it's dumping the contents of the params hash to the response page without going through the page view. 它在这里用于调试目的,它将params散列的内容转储到响应页面而不通过页面视图。

render :text => "hello world!"

Renders the clear text "hello world" with status code 200 使用状态代码200呈现明文“hello world”

This is what the :text => ... means refer http://api.rubyonrails.org/classes/ActionController/Base.html 这就是:text => ...意思是参考http://api.rubyonrails.org/classes/ActionController/Base.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM