English 中文(简体)
我如何在铁路帮手用红宝石格式化json数据?
原标题:How do I format json data with ruby on rails helpers?

过去几天我一直在研究 并熟悉手柄, js Tylating和它很好。

现在,我正在和我的一个部分一起工作,这个部分有一堆 html, 在那里我曾经使用过一些助手, 比如简单的_format, time_ago_in_words 等。 显然这些助手不能用在把手栏上。 所以我想做这样的事情:

def get_micropost

   respond_to do |format|   
     format.json { render json: formatted_micropost_json_data(Micropost.where("id < ?", params[:micropost_id]).first) } 
   end

end

<强力 > 调解站助手:

module MicropostsHelper

    def formatted_micropost_json_data(micropost)

        content: simple_format h(micropost.content)
        created_at: time_ago_in_words(micropost.created_at)
        id: micropost.id
        image: micropost.image
        link: micropost.link
        poster_id: micropost.poster_id
        updated_at: micropost.updated_at 
        user_id: micropost.user_id 

    end

end

所以当我通过ajax调用机将 JSON 调回时, 它将会被正确格式化 。 然后, 我就可以简单的将我的控管栏变量显示为正常 。

这行得通吗?

如果不是,什么是这样做的最佳方式?

谨请注意:

最佳回答

我在此建议重写您的代码 :

class Micropost
  def formatted_json_data
    {
      content:    simple_format(h(self.content)),
      created_at: time_ago_in_words(self.created_at),
      id:         self.id,
      image:      self.image,
      link:       self.link,
      poster_id:  self.poster_id,
      updated_at: self.updated_at,
      user_id:    self.user_id
    }
  end
end

def get_micropost
  respond_to do |format|   
    format.json do
      posts = Micropost.where("id < ?", params[:micropost_id])
      data = posts.first.formatted_json_data
      render(json: data)
    end
  end
end

你本想用你的方法 做一个粗略的文字 - -你原先的不是 合成有效红宝石代码。

此外,打破一长串连锁方法电话,可能会增加信息错误信息,提供处理错误信息的更明确机会。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签