本文介绍了Rails 渲染 json 包括带参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在方法控制器中有这个渲染:
I have this render in a method controller:
render :json => @boats, :include => {:port => {:only => [:id, :name], :include => {:city => {:only => [:id, :name], :methods => :translatedCity}}}}
但是模型中的 translatedCity
方法应该接受语言参数.
but translatedCity
method in the model should accept language parameter.
def translatedCity
language = params[:language]
puts "translatedCity.language=" + language
city = TranslationService.translateCity(self.city_id, language)
return city.element_translation
end
我在控制器中有参数.有没有办法把这个参数传递给模型中的方法?
I have the parameter in the controller. Is there any way to pass this parameter to the method in the model?
推荐答案
所有答案都很好!
我只是做了类似的事情:
I just did something like:
在控制器中:
boat.port.language = params[:language]
在模型中:
attr_accessor :language
@language
def translatedCity
city = TranslationService.translateCity(self.city_id, language)
return city.element_translation
end
你怎么看?
这篇关于Rails 渲染 json 包括带参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!