Rails how to change attribute name when rendering json?

后端 未结 2 649
执念已碎
执念已碎 2021-01-02 17:01

In my controller I have:

@pakkes = Pakke.where(\"navn like ?\", \"%#{params[:q]}%\")

respond_to do |format|
  format.html # index.html.erb
  format.xml  {          


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 17:37

    Override the as_json method. It's used by to_json in order to produce the output. You can do something like:

    def as_json options={}
     {
       name: navn,
       .... # other attributes you want to add to json
     }
    end
    

提交回复
热议问题