使用 Rails 渲染特定字段

发布于 2024-08-25 14:32:09 字数 429 浏览 4 评论 0原文

如果我有一个对象

@user

,并且我只想渲染其中的某些字段,例如名字和姓氏(我正在使用 AMF),

render :amf => @user

例如我有一个 @user 的属性,它是“dob”(出生日期),我想在控制器业务逻辑中使用它,但我不想将其发送到客户端(在本例中为 Flex),我当然可以在渲染之前执行类似的操作:

@user.dob = nil

但我认为必须有更好的方法来执行此操作。

我该怎么做?

我知道我可以在执行“查找”时使用 :select 但我需要在服务器端使用其他字段,但不想将它们与 AMF 一起发送到客户端,而且我不想再做一次找到'

谢谢,

If I have an object say

@user

and I want to render only certain fields in it say first_name and last_name(I'm using AMF)

render :amf => @user

For instance I have a property for @user which is 'dob' (date of birth) I would like to use it inside the controller business logic but I don't want to send it to the client (in this case Flex) I can defenitaly do something like this before rendering:

@user.dob = nil

But I thought there must be a better way of doing this.

how do I do that?

I know I can use :select when doing the 'find' but I need to use the other field at the server side but don't want to send them with AMF to the client side and I don't want to do a second 'find'

Thanks,

Tam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-09-01 14:32:09

这篇文章提供了详细信息的方法。

您已按如下方式配置 config/rubyamf_config.rb 文件:

require 'app/configuration'
module RubyAMF
  module Configuration
    ClassMappings.ignore_fields   = ['created_at','updated_at']
    ClassMappings.translate_case  = true
    ClassMappings.assume_types    = false
    ParameterMappings.scaffolding = false

    ClassMappings.register(
      :actionscript  => 'User',
      :ruby          => 'User',
      :type          => 'active_record',
      :associations  => ["employees"],
      :ignore_fields => ["dob"]
      :attributes    => ["id", "name", "location", "created_at", "updated_at"]
    )

    ClassMappings.force_active_record_ids = true
    ClassMappings.use_ruby_date_time = false
    ClassMappings.use_array_collection = true
    ClassMappings.check_for_associations = true
    ParameterMappings.always_add_to_params = true
   end
end

This article gives the details for the approach.

You have configure the config/rubyamf_config.rb file as follows:

require 'app/configuration'
module RubyAMF
  module Configuration
    ClassMappings.ignore_fields   = ['created_at','updated_at']
    ClassMappings.translate_case  = true
    ClassMappings.assume_types    = false
    ParameterMappings.scaffolding = false

    ClassMappings.register(
      :actionscript  => 'User',
      :ruby          => 'User',
      :type          => 'active_record',
      :associations  => ["employees"],
      :ignore_fields => ["dob"]
      :attributes    => ["id", "name", "location", "created_at", "updated_at"]
    )

    ClassMappings.force_active_record_ids = true
    ClassMappings.use_ruby_date_time = false
    ClassMappings.use_array_collection = true
    ClassMappings.check_for_associations = true
    ParameterMappings.always_add_to_params = true
   end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文