简体   繁体   中英

What does this line mean in Ruby?

def show
  render :text => params.inspect
end

What is render :text => ?

What is render , :text , and the => ?
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.

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. 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). 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"

I think you should really be reading the ruby getting started guides before digging too deep in to rails.

The render :text idiom is for rendering text directly to the response, without any view. 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.

render :text => "hello world!"

Renders the clear text "hello world" with status code 200

This is what the :text => ... means refer http://api.rubyonrails.org/classes/ActionController/Base.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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