Rails 100% newb issue - send() method

前端 未结 3 1534
梦如初夏
梦如初夏 2021-01-31 08:52

Could someone please help me to understand what the \'send()\' method listed below is used for? The code below, when I am reading it, makes no sense what purpose it\'s serving.<

3条回答
  •  一个人的身影
    2021-01-31 09:12

    send is used to pass a method (and arguments) to an object. It's really handy when you don't know in advance the name of the method, because it's represented as a mere string or symbol.

    Ex: Performer.find(params[:performer_id]) is the same as Performer.send(:find, params[:performer_id])

    Beware here because relying on params when using send could be dangerous: what if users pass destroy or delete? It would actually delete your object.

提交回复
热议问题