大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 如何在Controller层上使用Rails image_url helper以便它返回正确的值?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class Api::Mobile::HomeController < Api::Mobile::ApplicationController include ActionView::Helpers::AssetUrlHelper def index @items = [Api::Mobile::HomePageItem.new( type: 'image',image: image_url("api/mobile/home/tutor-with-student.jpg"))] end end
图像tutor-with-student.jpg存在于以下文件夹中:
app/assets/images/api/mobile/home/tutor-with-student.jpg
问题是image_url返回值:
http://myhost.com/images/api/mobile/home/tutor-with-student.jpg
代替
http://myhost.com/assets/api/mobile/home/tutor-with-student.jpg
请注意,当我从实际视图中使用image_url时,该方法返回正确的值.
如何在Controller层上使用image_url方法以便返回正确的值?
ActionController::Base.Helpers.asset_url("api/mobile/home/tutor-with-student.jpg",type: :imagE)
并删除包含该模块
::的ActionView ::助手AssetUrlHelper
此外,请确保已在环境配置文件中设置action_controller.asset_host值.因此,对于您的生产环境,它应该在config / environments / production.rb中,它必须是这样的
config.action_controller.asset_host='myhost.com'
以上是大佬教程为你收集整理的ruby-on-rails – 如何在Controller层上使用Rails image_url helper以便它返回正确的值?全部内容,希望文章能够帮你解决ruby-on-rails – 如何在Controller层上使用Rails image_url helper以便它返回正确的值?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。