What does this line mean in Ruby?

def show
  render :text => params.inspect
end

What is render :text =>?

What it is render, :textand =>?
Are they standard rubies?

+3
source share
5 answers

The syntax you see in this code snippet is not limited render(), but it is common in many other Ruby on Rail methods.

The method accepts a hash map using simplified syntax.
Code equivalent

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

Other pieces of code that contain the same syntax:

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
+5
source

render - API- rails. . doc. -, .

+3

, ,

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

, , Hash- ( ). 1 : text (: , , ), "hello world"

, , .

+2

render: text , - . , params , .

+1
render :text => "hello world!"

"hello world" 200

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

+1
source

Source: https://habr.com/ru/post/1761257/


All Articles