Referencing Model with String input

后端 未结 1 1740
感情败类
感情败类 2021-02-05 19:10

Lets say I wish to make a page that can query the desired object with type(string) and id(int).

/query?type=people&id=1

would fe

1条回答
  •  一向
    一向 (楼主)
    2021-02-05 19:45

    Try:

    klass = params[:type]
    klass.singularize.classify.constantize.find(params[:id])
    

    Edit:

    @object = params[:type].singularize.classify.constantize.find(params[:id])
    

    0 讨论(0)
提交回复
热议问题